javascript chromedriver 清除缓存 - java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33529585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:36:41  来源:igfitidea点击:

chromedriver clear cache - java

javascriptjavaseleniumselenium-chromedriver

提问by Ben

How can I clear the cache of a new instance of a chromedriver in java? I am trying this but im not too sure what else to do? Would it be possible to create a javascript hack to clear the cache in JS which I can call from my driver?

如何清除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;
}

回答by alecxe

By default, a completely clean profile with an empty cache, local storage, cookies is fired up by selenium. You are actually browsing privatelywith selenium.

默认情况下,一个带有空缓存、本地存储、cookie 的完全干净的配置文件由 selenium 启动。您实际上是在使用 selenium 进行私密浏览

First of all, there is a problem in your code - you are not passing your DesiredCapabilitiesinstance to the webdriver constructor (not tested though):

首先,您的代码存在问题 - 您没有将DesiredCapabilities实例传递给 webdriver 构造函数(虽然未测试):

ChromeDriver driver = new ChromeDriver(capabilities);

You may also try 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);