【问题标题】:Merge DesiredCapabilities with FirefoxOptions in Selenium Webdriver在 Selenium Webdriver 中将 DesiredCapabilities 与 FirefoxOptions 合并
【发布时间】:2019-03-20 05:18:22
【问题描述】:

我在 C# 中使用 Selenium webdriver 和代理以及一些附加属性,如下所示

FirefoxOptions options = new FirefoxOptions();
options.AddArguments("disable-infobars");

String proxyServer= "192.168.1.8:808";
Proxy proxy = new Proxy();

proxy.HttpProxy = proxyServer;
proxy.FtpProxy = proxyServer;
proxy.SslProxy = proxyServer;

DesiredCapabilities cap = new DesiredCapabilities();
cap.SetCapability(CapabilityType.Proxy, proxy);

var firefoxDriverService = FirefoxDriverService.CreateDefaultService();
firefoxDriverService.HideCommandPromptWindow = true;

Webdriver实例初始化为

IWebDriver driver = new FirefoxDriver(firefoxDriverService, options, TimeSpan.FromSeconds(600));

我唯一不能做的就是将 DesiredCapabilities 与 FirefoxOptions 合并以使用代理。所以我想做这样的事情

options.SetCapability(cap);

有办法吗?

【问题讨论】:

    标签: c# selenium selenium-webdriver webdriver selenium-chromedriver


    【解决方案1】:

    您可以将Proxy 添加到FirefoxOptiions 而不是DesiredCapabilities,如下所示:

    FirefoxOptions options = new FirefoxOptions();
    options.AddArguments("disable-infobars");
    
    String proxyServer= "192.168.1.8:808";
    Proxy proxy = new Proxy();
    
    proxy.HttpProxy = proxyServer;
    proxy.FtpProxy = proxyServer;
    proxy.SslProxy = proxyServer;
    
    options.Profile.SetProxyPreferences(proxy); //this line is crucial
    

    【讨论】:

      【解决方案2】:

      你试过options.AddAdditionalCapability(CapabilityType.Proxy, proxy);吗?

      来自source

      public class FirefoxOptions : DriverOptions
      ...
              /// <summary>
              /// Provides a means to add additional capabilities not yet added as type safe options
              /// for the Firefox driver.
              /// </summary>
              /// <param name="optionName">The name of the capability to add.</param>
              /// <param name="optionValue">The value of the capability to add.</param>
              /// <exception cref="ArgumentException">
              /// thrown when attempting to add a capability for which there is already a type safe option, or
              /// when <paramref name="optionName"/> is <see langword="null"/> or the empty string.
              /// </exception>
              /// <remarks>Calling <see cref="AddAdditionalFirefoxOption(string, object)"/>
              /// where <paramref name="optionName"/> has already been added will overwrite the
              /// existing value with the new value in <paramref name="optionValue"/>.
              /// Calling this method adds capabilities to the Firefox-specific options object passed to
              /// geckodriver.exe (property name 'moz:firefoxOptions').</remarks>
              public void AddAdditionalFirefoxOption(string optionName, object optionValue)
              {
                  this.ValidateCapabilityName(optionName);
                  this.additionalFirefoxOptions[optionName] = optionValue;
              }
      ...
      

      【讨论】:

      • 我希望尝试使用具有“代理”功能名称的AddAdditionalCapability 会引发异常,就像您尝试使用已知功能名称时一样。
      【解决方案3】:

      如果您所做的只是使用标准的Proxy 类型来为实例化的 Firefox 实例设置代理设置,那么现代版本的 .NET 绑定提供了一个 FirefoxOptions.Proxy 属性,该属性将被集成到你的代码如下:

      FirefoxOptions options = new FirefoxOptions();
      options.AddArguments("disable-infobars");
      
      String proxyServer= "192.168.1.8:808";
      Proxy proxy = new Proxy();
      
      proxy.HttpProxy = proxyServer;
      proxy.FtpProxy = proxyServer;
      proxy.SslProxy = proxyServer;
      
      options.Proxy = proxy;
      
      var firefoxDriverService = FirefoxDriverService.CreateDefaultService();
      firefoxDriverService.HideCommandPromptWindow = true;
      
      WebDriver driver = new FirefoxDriver(firefoxDriverService, options, TimeSpan.FromSeconds(600));
      

      应该不需要像接受的答案那样在用户配置文件中设置代理属性。

      【讨论】:

      • Visual Studio 中有没有一种方法可以在应用程序中管理代理,还是必须单独设置一些东西?我看到一些关于使用 Selenium 设置代理的随机内容,但没有关于实际代理服务器本身的内容。几乎使它看起来像代理服务器是 Selenium 的一部分,并且可以自己启动或其他东西。我正在尝试使用 Selenium 来监控加载网站的所有流量,以查找正在加载的包含某些单词的特定 URL。
      • 代理服务器将是一个独立于 Selenium 的项目。我已经写了severalblogposts,关于在 C# 中使用代理,使用几个不同的软件代理。
      猜你喜欢
      • 2020-05-22
      • 2013-07-05
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 2017-01-06
      相关资源
      最近更新 更多