Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 253
Mark Micallef Avatar asked Aug 04 '11 11:08

Mark Micallef


3 Answers

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 ;)

like image 67
NeuronQ Avatar answered Oct 11 '22 02:10

NeuronQ


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 = []
like image 37
dgrandes Avatar answered Oct 11 '22 00:10

dgrandes


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);
like image 23
David Avatar answered Oct 11 '22 02:10

David