我在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);
有办法吗?
答案 0 :(得分: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;
}
...
答案 1 :(得分:0)
您可以像这样将DesiredCapabilities
添加到Proxy
而不是FirefoxOptiions
:
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 :(得分: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));
无需像接受的答案那样在用户个人资料中设置代理属性。