硒:清除Chrome缓存

VISHVAMBRUTHJAVAGALTHIMMEGOWDA

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

有人可以建议我单击Chrome中的“清除数据”按钮吗?我已经编写了以下代码,但是该代码无法正常工作。

配置:

Chrome版本:65.0.3325.181版(正式版本)(64位)

硒版本: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();

在此处输入图片说明

你在这里用

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

不幸的是,这不会起作用,因为Chrome设置页面使用PolymerWebComponents,需要使用通过/ deep /组合器使用的查询选择器,因此这种情况下的选择器为* /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);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章