Question

I tried to modify the user agent string using Open Browser with desired_capabilities and discovered that Chrome does not support that technique any more.

After much searching and reading I discovered that there was a new version of Selenium2library that had a new keyword Create Webdriver that is supposed to address this issue.

I modified their example to suite my needs. But no matter what I do, it does not modify the user agent string.

I get no errors, no warnings, no nothing, except a perfectly working browser without a modified user agent string.

I tried to modify other options like --start-maximized with the same result, i.e. no result at all.

Excerpt from keyword that opens Google Chrome and (allegedly) modifies the user agent string:

${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()   sys, selenium.webdriver
${options.add_argument}=    Set Variable    user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36 System/ComputerId" 
Create WebDriver    Chrome  chrome_options=${options}   
Go To   http://www.useragentstring.com

Fashioned after the example given here (at the bottom of the page):

https://github.com/rtomac/robotframework-selenium2library/issues/225

My software setup:

  • Google Chrome 31.0.1650.59
  • Selenium 2.39.0
  • Selenium2library 1.5
  • Robot Framework 2.8.3
  • Robot Framework Ride 1.2.2

So what is the problem?

Was it helpful?

Solution

After some more tinkering and reading I managed to figure out a way to get the example working.

Instead of using ${options.add_argument}= I used Call Method ${options} add_argument.

${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
Call Method    ${options}    add_argument    --user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36 System/ComputerId
Create WebDriver    Chrome    chrome_options=${options}    
Go To    http://www.useragentstring.com

OTHER TIPS

I tried setting up user agent in chrome using RF and working fine for me below snippet...!

${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
${userAgent}=  set variable  --user-agent="Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
Call Method    ${options}  add_argument  ${userAgent}
Create WebDriver    Chrome    chrome_options=${options}

I have encountered a similar problem. i tried running your code but no luck in making it work. It just says that the user agent is not defined. I have browsed and came across this code, but unfortunately is written in python:

from selenium import webdriver
import webbrowser
from selenium.webdriver.chrome.options import Options
mobile_emulation = {
    "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
    "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options = chrome_options)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top