如何使用.net更改selenium中的用户代理

时间:2013-08-01 15:22:35

标签: .net selenium webdriver selenium-webdriver user-agent

我想用不同的代理(iPhone,iPad,Android)测试用.NET编写的Web应用程序,我正在使用NUnit进行测试和Selenium。

是否有人使用c#或VB在Selenium中更改代理(例如iPad或iPhone)?

2 个答案:

答案 0 :(得分:26)

示例UA(您可能需要Google自己找到适合您目的的那些):

  

Mozilla / 5.0(iPad; CPU OS 6_0,如Mac OS X)AppleWebKit / 536.26(KHTML,类似Gecko)版本/ 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 (未经测试的代码):

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:很抱歉,我认为这不是Selenium原生的。

进一步阅读:Set user agent using Selenium WebDriver C# and Ruby

答案 1 :(得分:3)

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