[英]Unable to launch Chrome in incognito mode using Selenium WebDriver using C#
I am trying to launch Chrome using Selenium WebDriver in incognito mode, but not able to accomplish. 我正在尝试使用Selenium WebDriver在隐身模式下启动Chrome,但无法完成。 I tried all options but not able to launch. 我尝试了所有选项,但无法启动。 Below is my code snippet 以下是我的代码段
case "chrome":
ChromeOptions options = new ChromeOptions();
options.AddArgument("--incognito"); //Line XYZ
desiredCapabilities = DesiredCapabilities.Chrome();
desiredCapabilities.SetCapability(ChromeOptions.Capability, options);
break;
var capabilities = BuildDesiredCapabilities();
webDriver = new RemoteWebDriver(new Uri(gridHubURL), capabilities,
TimeSpan.FromSeconds(ApplicationConfiguration.RemoteDriverTimeOutValue));
Can anyone please help me what I am doing wrong here? 有人可以帮我在这里做错什么吗? I also tried the below code options in Line XYZ 我也在XYZ行中尝试了以下代码选项
Any pointers will be much helpful. 任何指针将大有帮助。
EDIT1 Please find the updated code here. EDIT1请在此处找到更新的代码。
public IWebDriver CreateDriver()
{
var capabilities = BuildDesiredCapabilities();
webDriver = new RemoteWebDriver(new Uri(gridHubURL), capabilities,
TimeSpan.FromSeconds(ApplicationConfiguration.RemoteDriverTimeOutValue));
webDriver.Manage().Window.Maximize();
webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(ApplicationConfiguration.TimeOutValue));
webDriver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(ApplicationConfiguration.TimeOutValue));
return webDriver;
}
private DesiredCapabilities BuildDesiredCapabilities()
{
DesiredCapabilities desiredCapabilities;
switch (browserName.ToLower())
{
case "firefox":
desiredCapabilities = DesiredCapabilities.Firefox();
break;
case "chrome":
desiredCapabilities = DesiredCapabilities.Chrome();
desiredCapabilities.SetCapability("chrome.switches", "--incognito");
break;
case "ie":
desiredCapabilities = DesiredCapabilities.InternetExplorer();
desiredCapabilities.SetCapability("ie.ensureCleanSession", true);
break;
default:
desiredCapabilities = DesiredCapabilities.Firefox();
break;
}
return desiredCapabilities;
}
The .NET bindings have introduced browser-specific Options
classes to avoid having to know or understand arbitrary capability values. .NET绑定引入了特定于浏览器的Options
类,以避免必须了解或理解任意功能值。 You were using just such a class, ChromeOptions
, in your original code. 您在原始代码中只使用了此类ChromeOptions
。 You missed one extra step, though, in how to use the ChromeOptions
class with RemoteWebDriver
. 但是,您没有在如何将ChromeOptions
类与RemoteWebDriver
使用方面又ChromeOptions
了RemoteWebDriver
。 The missing piece is that you should use the ToCapabilities()
method to convert the ChromeOptions
object to the ICapabilities
object that RemoteWebDriver
expects. 缺少的部分是您应该使用ToCapabilities()
方法将ChromeOptions
对象转换为RemoteWebDriver
期望的ICapabilities
对象。 Your code would look something like the following: 您的代码如下所示:
var options = new ChromeOptions();
options.AddArgument("incognito");
var capabilities = options.ToCapabilities();
var driver = new RemoteWebDriver(new URI(gridHubURL), capabilities);
You should pass parameters to the executable like this: 您应该像这样将参数传递给可执行文件:
desiredCapabilities = DesiredCapabilities.Chrome();
desiredCapabilities.SetCapability("chrome.switches", "--incognito");
So passing the parameter --incognito
to the chrome.switches
capability should work. 因此,将参数--incognito
传递给chrome.switches
功能应该有效。
NOTE: 注意:
The chrome.switches
capability has been deprecated for over two years. chrome.switches
功能已弃用两年多了。 A list of the current supported capabilities can be found at the official chromedriver Google Sites page . 可以在官方chromedriver Google站点页面上找到当前支持的功能列表。 Additionally, use of arbitrary capabilities has been discouraged by the Selenium project for some time, particularly when using the .NET bindings 此外,Selenium项目不鼓励使用任意功能一段时间,特别是在使用.NET绑定时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.