Selenium:清除铬缓存

VIS*_*WDA 7 selenium google-chrome

在我的应用程序中,我需要一种方法在注销之前仅清除chrome浏览器的缓存(除了cookie - 我不想删除cookie).

任何人都可以建议我点击chrome中的CLEAR DATA按钮.我写了下面的代码,但代码不起作用.

配置:

Chrome版本:65.0.3325.181版(官方版)(64位)

Selenium版本:3.11.0

//Clear the cache for the ChromeDriver instance.
driver.get("chrome://settings/clearBrowserData");
Thread.sleep(10000);
driver.findElement(By.xpath("//*[@id='clearBrowsingDataConfirm']")).click();
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Sod*_*ium 8

你在这里用

driver.findElement(By.xpath("//*[@id='clearBrowsingDataConfirm']")).click();

遗憾的是,这不起作用,因为Chrome设置页面使用PolymerWebComponents,需要使用/ deep/combinator使用查询选择器,因此在这种情况下选择器是* /deep/ #clearBrowsingDataConfirm.

以下是您的问题的解决方法......您可以使用以下任一方法实现相同的目标......

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

public class ClearChromeCache {

    WebDriver driver;

    /*This will clear cache*/
    @Test
    public void clearCache() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","C://WebDrivers/chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("disable-infobars");
        chromeOptions.addArguments("start-maximized");
        driver = new ChromeDriver(chromeOptions);
        driver.get("chrome://settings/clearBrowserData");
        Thread.sleep(5000);
        driver.switchTo().activeElement();
        driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")).click();
        Thread.sleep(5000);
    }

    /*This will launch browser with cache disabled*/
    @Test
    public void launchWithoutCache() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver","C://WebDrivers/chromedriver.exe");
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability("applicationCacheEnabled", false);
        driver = new ChromeDriver(cap);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案也不再有效:https://developers.google.com/web/updates/2017/10/remove-shadow-piercing (3认同)

Bla*_*ise 8

Chrome 支持 DevTools 协议命令,例如Network.clearBrowserCache文档)。默认情况下,Selenium 没有此专有协议的接口。

您可以通过扩展 Selenium 的命令来添加支持:

driver.command_executor._commands['SEND_COMMAND'] = (
    'POST', '/session/$sessionId/chromium/send_command'
)
Run Code Online (Sandbox Code Playgroud)

这是你如何使用它:

driver.execute('SEND_COMMAND', dict(cmd='Network.clearBrowserCache', params={}))
Run Code Online (Sandbox Code Playgroud)

注意:此示例适用于 Python 的 Selenium,但也可以通过扩展命令以类似的方式在其他平台的 Selenium 中实现。


小智 6

2020 年解决方案(使用 Selenium 4 alpha):

使用开发工具

    private void clearDriverCache(ChromeDriver driver) {
    driver.getDevTools().createSessionIfThereIsNotOne();
    driver.getDevTools().send(Network.clearBrowserCookies());
    // you could also use                
    // driver.getDevTools().send(Network.clearBrowserCache());
}
Run Code Online (Sandbox Code Playgroud)