在使用 Java 运行一些 Selenium WebDriver 测试之前清除缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32970855/
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
Clear cache before running some Selenium WebDriver tests using Java
提问by Uziii
I am working on Selenium WebDriver automation in java programming language. In my test suite that initiates the browser window once and perform all the tests. I want to clear the browser cache before running some tests without restarting the browser. Is there any command/function, that can achieve the purpose? Thanks.
我正在 Java 编程语言中研究 Selenium WebDriver 自动化。在我的测试套件中,一次启动浏览器窗口并执行所有测试。我想在不重新启动浏览器的情况下运行一些测试之前清除浏览器缓存。是否有任何命令/功能可以达到目的?谢谢。
回答by vins
For IE
IE浏览器
DesiredCapabilities ieCap = DesiredCapabilities.internetExplorer();
ieCap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
For Chrome:
对于铬:
https://code.google.com/p/chromedriver/issues/detail?id=583
https://code.google.com/p/chromedriver/issues/detail?id=583
To delete cookies:
要删除 cookie:
driver.manage().deleteAllCookies();
回答by Rodrigo Estevao
At least in Chrome, I strongly believe that if you go incognito you wont to have to clean up your cookies. You can set your options like following (the :
至少在 Chrome 中,我坚信如果你隐身,你就不必清理你的 cookie。您可以设置如下选项(以下内容:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def _options():
options = Options()
options.add_argument('--ignore-certificate-errors')
#options.add_argument("--test-type")
options.add_argument("--headless")
options.add_argument("--incognito")
options.add_argument('--disable-gpu') if os.name == 'nt' else None # Windows workaround
options.add_argument("--verbose")
return options
and call like this:
并像这样调用:
with webdriver.Chrome(options=options) as driver:
driver.implicitly_wait(conf["implicitly_wait"])
driver.get(conf["url"])
回答by An Khang
This is what I use in Python:
这是我在 Python 中使用的:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('chrome://settings/clearBrowserData')
driver.find_element_by_xpath('//settings-ui').send_keys(Keys.ENTER)
You can try converting these into Java. Hope this will help! :)
您可以尝试将这些转换为 Java。希望这会有所帮助!:)
回答by ItwasJJsmile
The following code is based on @An Khang 's answers. and it is working properly on Chrome 78.
以下代码基于@An Khang 的回答。它在 Chrome 78 上正常工作。
ChromeDriver chromeDriver = new ChromeDriver();
chromeDriver.manage().deleteAllCookies();
chromeDriver.get("chrome://settings/clearBrowserData");
chromeDriver.findElementByXPath("//settings-ui").sendKeys(Keys.ENTER);
return chromeDriver;