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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With