Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change user agent in selenium with .net

Tags:

I want to test a web app written in .NET with different agents (iPhone, iPad, Android), I'm using NUnit for the tests and Selenium.

Anyone has a sample to change the agent (for example for iPad or iPhone) in Selenium with c# or VB?

like image 937
Luis Molina Avatar asked Aug 01 '13 15:08

Luis Molina


Video Answer


2 Answers

Example UA (You may need Google yourself to find the ones suit your purpose):

Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25

C# Firefox:

FirefoxProfile profile = new FirefoxProfile(); profile.SetPreference("general.useragent.override", "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25"); IWebDriver driver = new FirefoxDriver(profile); 

C# Chrome:

ChromeOptions options = new ChromeOptions(); options.AddArgument("--user-agent=Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25"); IWebDriver driver = new ChromeDriver(options); 

C# PhantomJS (untested code):

PhantomJSOptions options = new PhantomJSOptions(); options.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25"); IWebDriver driver = new PhantomJSDriver(options); 

C# IE: Sorry, I don't think this is possible natively from Selenium.

Further reading: Set user agent using Selenium WebDriver C# and Ruby

like image 98
Yi Zeng Avatar answered Sep 20 '22 16:09

Yi Zeng


In c#:

public static FirefoxProfile myFireProfile = new FirefoxProfile(); myFireProfile.SetPreference("general.useragent.override", "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10"); public IWebDriver driver= new FirefoxDriver(myFireProfile); 
like image 40
CIGuy Avatar answered Sep 17 '22 16:09

CIGuy