简体   繁体   English

无法使用C#使用Selenium WebDriver以隐身模式启动Chrome

[英]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使用方面又ChromeOptionsRemoteWebDriver 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.

相关问题 Selenium WebDriver - Chrome - C# - 无法以隐身模式启动selenium浏览器作为最大化的浏览器 - Selenium WebDriver - Chrome - C# - Unable to launch selenium browser in Incognito Mode as a maximised browser 在 C# 中使用 Selenium Chrome Webdriver 时出错 - Error using Selenium Chrome Webdriver with C# 无法使用带有C#的Selenium Webdriver启动IE浏览器 - Not able to launch IE browser using Selenium webdriver with c# 如何使用C#Selenium在移动模拟器横向模式下启动Chrome? - How to launch Chrome in mobile emulator Landscape mode using C# Selenium? 在 C# 中离线时使用 Selenium-WebDriver 测试 Chrome - Testing Chrome using Selenium-WebDriver while offline in C# 如何使用Selenium Webdriver C#在chrome弹出窗口上单击OK - How to Click OK on chrome popup using Selenium Webdriver C# 使用带有C#的Selenium Webdriver在弹出窗口中找不到元素 - Unable to find the Element on the pop up using Selenium Webdriver with C# 使用C#仅关闭Google Chrome隐身窗口 - using C# to close Google chrome incognito windows only C#-Selenium WebDriver无法打开Chrome浏览器窗口 - C# - selenium webdriver unable to open chrome browser window 如何使用不同的 chrome 配置文件 C#、selenium webdriver 并行触发多个 chrome 实例 - How to trigger multiple chrome instances in parallel using different chrome profiles C#, selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM