简体   繁体   中英

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. 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

Any pointers will be much helpful.

EDIT1 Please find the updated code here.

 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. You were using just such a class, ChromeOptions , in your original code. You missed one extra step, though, in how to use the ChromeOptions class with RemoteWebDriver . The missing piece is that you should use the ToCapabilities() method to convert the ChromeOptions object to the ICapabilities object that RemoteWebDriver expects. 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.

NOTE:

The chrome.switches capability has been deprecated for over two years. A list of the current supported capabilities can be found at the official chromedriver Google Sites page . Additionally, use of arbitrary capabilities has been discouraged by the Selenium project for some time, particularly when using the .NET bindings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM