c# - Selenium 如何使用 chromedriver 和 C# 清除缓存

标签 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);

最佳答案

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

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

关于c# - Selenium 如何使用 chromedriver 和 C# 清除缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38335264/

相关文章:

C# : how to create delegate type from delegate types?

c# - 对枚举数组进行排序

java - Selenium Java - 如何选择没有 ID 的下拉元素

maven - 在 Spock 测试中无法启动 chromedriver

c# - 如何使用 XAML/C# 在我的 UWP 应用程序中允许双向数据绑定(bind)?

c# - 在 ListViewItem UWP C# 中点击图像

c# - 环视组中的条件如何在 .NET 正则表达式中工作?

selenium - Selenium 的 WebElement.getRect() 是否适用于 Chrome 或 IE?

Selenium .JavascriptException : javascript error: Failed to execute 'elementFromPoint' on 'Document' : The provided double value is non-finite

python - 用于定位文本区域元素的 Selenium 脚本不起作用