Selenium如何使用chromedriver和C#

时间:2016-07-12 17:17:38

标签: c# selenium-chromedriver

我发现Java示例解释清除缓存就像这样简单:

示例代码:

System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);

        ChromeOptions opts = new ChromeOptions();
        opts.addArguments("start-maximized");
        capabilities.setCapability(ChromeOptions.CAPABILITY, opts);

        WebDriver driver = new ChromeDriver(capabilities);
        driver.manage().deleteAllCookies();
        driver.get("http://www.google.com");
        System.out.println(driver.getTitle());
        driver.quit();

我尝试在C#4.6和Selenium WebDriver 2.45中执行相同操作,但DesiredCapabilities不包含ENSURING_CLEAN_SESSION的枚举或字符串。我不明白为什么C#版本没有与java版本相同的选项。我的预感是有一个技巧,比如直接使用代表java中可用的相同功能的字符串作为枚举,我只是不确定它是什么。

这是我的代码:

// SetCapability(..)方法调用没有意义......

var chromeDriverService = ChromeDriverService.CreateDefaultService();
                chromeDriverService.HideCommandPromptWindow = true;

                DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
                capabilities.SetCapability(ChromeOptions.Capability, true); // ? can't set ENSURING_CLEAN_SESSION, intellisense shows it does not exist

                var options = new ChromeOptions();

                options.AddArguments("test-type");                  

                return new ChromeDriver(chromeDriverService, options);

1 个答案:

答案 0 :(得分:0)

您可以简单地强制Chrome以隐身模式启动

capabilities.SetCapability("chrome.switcher", new [] {"--incognito"});