开发者

Selenium 2 - Setting user agent for IE and Chrome

I need to change the user agent value in IE and Chrome for some of our tests开发者_运维问答. The only selenium 2 examples I've come across only work with FirefoxDriver.

Has anyone managed to change the user agent for IE and Chrome?

Mark


I know this is veery old by now, but I stumbled across it and a few seconds ago and I also found the real solution (at least for the latest version of Selenium).

So here we go (Python, example faking the iPad UA):

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--user-agent=Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3')

driver = webdriver.Chrome(chrome_options=options)

# ...loads of fun...

I hope this is helpful for anyone else having the same problem. Oh, and it also works with all the other Chrome command line options. Njoy ;)


This is how I got it running in python for Chrome.

 from selenium import webdriver 
 ...
 def setUp(self):
        capabilities = webdriver.DesiredCapabilities.CHROME
        capabilities["chrome.switches"] = ["--user-agent="+USER_AGENT_STRING] 
        cls.driver = webdriver.Chrome(executable_path="servers/chromedriver",desired_capabilities=capabilities)
        self.driver.implicitly_wait(5)
        self.verificationErrors = []


Here's an answer for PHP:

$options = new ChromeOptions();
$options->addArguments(['--user-agent=my fake user-agent string']);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = RemoteWebDriver::create($host,$capabilities);


I finally found out how to do that at least in chrome:

capabilities = webdriver.common.desired_capabilities.DesiredCapabilities.CHROME.copy()
capabilities['javascriptEnabled'] = True
options = webdriver.ChromeOptions()
options.add_argument('--user-agent=<YOUR USER AGENT HERE>')

driver = webdriver.Remote(command_executor='http://<YOUR SELENIUM HUB HERE>:4444/wd/hub',desired_capabilities=capabilities, options=options)

Sources: https://gist.github.com/thureos/2db0bc44589669a00c22a86503c80bbb https://seleniumhq.github.io/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html?highlight=remote

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜