在我的应用程序中,我需要一种方法在注销之前仅清除 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 设置页面使用Polymer and Web组件,需要使用 /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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)