硒:清除Chrome缓存 [英] Selenium: Clear chrome cache
问题描述
在我的应用程序中,我需要一种方法在注销之前仅清除chrome浏览器的缓存(cookie除外-我不想删除cookie).
In my application I need a way to clear only the cache of the chrome browser before log out (except cookies - I do not want to delete cookies).
有人可以建议我单击Chrome中的清除数据"按钮的方法吗? 我已经编写了以下代码,但是该代码无法正常工作.
Can any one suggest me a way to click on the CLEAR DATA button in chrome. I have written the below code but the code is not working.
配置:
Chrome版本:65.0.3325.181版(正式内部版本)(64位)
Chrome Version: Version 65.0.3325.181 (Official Build) (64-bit)
硒版本:3.11.0
Selenium Version: 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设置页面使用 Polymer 和 WebComponents ,需要通过/deep/组合器使用查询选择器,因此该选择器情况是* /deep/ #clearBrowsingDataConfirm
.
Unfortunately, this won’t work because the Chrome settings page uses Polymer and WebComponents, need to use query selector using the /deep/ combinator, so selector in this case is * /deep/ #clearBrowsingDataConfirm
.
这是解决您的问题的方法...您可以使用以下任一方法来实现相同的目标...
Here is workaround to your problem...you can achieve the same using either one of the following...
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);
}
}
这篇关于硒:清除Chrome缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!