如何在java中清除chromedriver的新实例的缓存?我正在尝试这个,但我不太确定还能做什么?是否有可能创建一个javascript hack来清除JS中的缓存,我可以从我的驱动程序调用?
private static WebDriver makeDriver() {
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
return driver;
}
答案 0 :(得分:7)
默认情况下,一个完全干净的配置文件,其中包含空缓存,本地存储,Cookie由selenium 启动。你实际上是browsing privately硒。
首先,您的代码中存在问题 - 您没有将DesiredCapabilities
实例传递给webdriver构造函数(尽管未经过测试):
ChromeDriver driver = new ChromeDriver(capabilities);
您也可以尝试forcing the "incognito" mode:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
ChromeDriver driver = new ChromeDriver(capabilities);