【问题标题】:how to change user agent in selenium with .net如何使用 .net 更改 selenium 中的用户代理
【发布时间】:2014-02-10 10:38:24
【问题描述】:

我想用不同的代理(iPhone、iPad、Android)测试一个用 .NET 编写的网络应用,我使用 NUnit 和 Selenium 进行测试。

谁有用 c# 或 VB 在 Selenium 中更改代理(例如 iPad 或 iPhone)的示例?

【问题讨论】:

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


    【解决方案1】:

    示例 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# 火狐:

    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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2020-10-10
        • 1970-01-01
        • 2019-08-12
        • 1970-01-01
        • 2015-12-18
        • 1970-01-01
        • 1970-01-01
        • 2018-09-08
        相关资源
        最近更新 更多