how to change user agent in selenium with .net

独自空忆成欢 提交于 2019-12-10 00:48:32

问题


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?


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/17998162/how-to-change-user-agent-in-selenium-with-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!