在Selenium Webdriver中将DesiredCapabilities与FirefoxOptions合并

问题描述 投票:0回答:3

我在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
3个回答
0
投票

而不是DesiredCapabilities你可以像这样添加ProxyFirefoxOptiions

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

0
投票

你尝试过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;
        }
...

0
投票

如果您所做的只是使用标准的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));

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

© www.soinside.com 2019 - 2024. All rights reserved.