![](/img/trans.png)
[英]How to clear IE cache webdriver Selenium Grid DesiredCapabilities and InternetExplorerOptions
[英]Merge DesiredCapabilities with FirefoxOptions in Selenium Webdriver
我在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);
有辦法嗎?
你有沒有嘗試過options.AddAdditionalCapability(CapabilityType.Proxy, proxy);
?
從來源 :
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;
}
...
您可以將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
如果您所做的只是使用標准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));
應該不需要在用戶配置文件中設置代理屬性,如在接受的答案中那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.