简体   繁体   English

在使用 Java 运行一些 Selenium WebDriver 测试之前清除缓存

[英]Clear cache before running some Selenium WebDriver tests using Java

I am working on Selenium WebDriver automation in java programming language.我正在使用 java 编程语言进行 Selenium WebDriver 自动化。 In my test suite that initiates the browser window once and perform all the tests.在我的测试套件中,启动浏览器 window 一次并执行所有测试。 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.谢谢。

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.您可以尝试将这些转换为 Java。 Hope this will help!希望这会有所帮助! :) :)

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();

At least in Chrome, I strongly believe that if you go incognito you wont to have to clean up your cookies.至少在 Chrome 中,我坚信如果你隐身,你就不必清理你的 cookie。 You can set your options like following (the :您可以设置您的选项,如下所示(:

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"])

The following code is based on @An Khang 's answers.以下代码基于@An Khang 的回答。 and it is working properly on Chrome 78.它在 Chrome 78 上正常工作。

ChromeDriver chromeDriver = new ChromeDriver();

    chromeDriver.manage().deleteAllCookies();
    chromeDriver.get("chrome://settings/clearBrowserData");
    chromeDriver.findElementByXPath("//settings-ui").sendKeys(Keys.ENTER);

    return chromeDriver;
    WebDriver driver = new ChromeDriver();
    driver.manage().deleteAllCookies();
    driver.get("chrome://settings/clearBrowserData");
    driver.findElement(By.xpath("//settings-ui")).sendKeys(Keys.ENTER);
import org.openqa.selenium.Keys;

you need to import the Keys in newer version and change the last line to findElement by xpath您需要在较新版本中导入 Keys 并通过 xpath 将最后一行更改为 findElement

WebDriver driver = new ChromeDriver();

driver.manage().deleteAllCookies();
driver.get("chrome://settings/clearBrowserData");
driver.findElement(By.xpath("//settings-ui")).sendKeys(Keys.ENTER);

On Google chrome you can use this script:在谷歌浏览器上你可以使用这个脚本:

 driver.get("chrome://settings/clearBrowserData"); JavascriptExecutor jse = (JavascriptExecutor)driver; WebElement clearData = (WebElement) jse.executeScript("return document.querySelector(\\"body > settings-ui\\").shadowRoot.querySelector(\\"#main\\").shadowRoot.querySelector(\\"settings-basic-page\\").shadowRoot.querySelector(\\"#basicPage > settings-section:nth-child(8) > settings-privacy-page\\").shadowRoot.querySelector(\\"settings-clear-browsing-data-dialog\\").shadowRoot.querySelector(\\"#clearBrowsingDataConfirm\\")"); ((JavascriptExecutor)driver).executeScript("arguments[0].click();", clearData);

what i found working for myself was adding chromium flag:我发现为自己工作的是添加铬标志:

--disk-cache-size=0 --disk-cache-size=0

not sure if it clears cache, but any cache related problems disappeared in my case不确定它是否清除缓存,但在我的案例中,任何与缓存相关的问题都消失了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium Webdriver测试在执行一段时间后停止运行 - Selenium webdriver tests stop running after some time of execution 使用Selenium WebDriver,Selenium Grid和testNG运行并行测试 - Running Parallel Tests using Selenium WebDriver, Selenium Grid and testNG 使用testng在一个浏览器中跨多个类运行Java Selenium Webdriver测试 - Running java selenium webdriver tests across many classes in one browser using testng 使用Web界面或任何可视化工具运行Selenium WebDriver测试 - Running Selenium WebDriver tests using Web interface or any Visual tool 使用相同的Firefox窗口在Selenium WebDriver(Java)中运行多个测试 - Using the same Firefox Window to run multiple tests in Selenium WebDriver (Java) 使用 Java 的 Selenium WebDriver 测试中的 waitForVisible/waitForElementPresent 是否等效? - Equivalent of waitForVisible/waitForElementPresent in Selenium WebDriver tests using Java? 如何处理-在缓存中找不到元素-自从在Selenium WebDriver Java测试中查找页面以来,页面可能已更改 - How to handle - Element not found in the cache - perhaps the page has changed since it was looked up in Selenium WebDriver Java tests 在负载下运行Selenium WebDriver测试 - Running Selenium WebDriver tests under the load 在 Java 中使用 Selenium WebDriver - Using Selenium WebDriver with Java 使用Java的Selenium Webdriver - Selenium Webdriver using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM