java - 如何在不同浏览器的 selenium 中自动清除无头模式(无 UI)的浏览历史记录?
问题描述
我正在自动化网络浏览器来测试我的需求(自动填充/完成,尤其是与持久性相关)。清除 cookie 与清除浏览器历史记录不同,对吧?是否可以在无头模式下自动清除浏览器历史记录?经过这么多的网上冲浪后,我的假设是它不是。(我读过一篇文章说,出于安全原因,不允许在无头模式下通过自动化清除浏览历史记录)。
此外,通过 selenium Web 驱动程序启动以测试案例的自动浏览器版本在注销帐户后不会立即存储任何缓存。假设我登录并退出我的帐户。我希望 selenium 在我刷新页面并键入它的几个起始字母时将我之前输入的用户名保留为自动填充(这在普通浏览器中会发生,但在自动浏览器版本中不会发生)。这可以实现吗?提前致谢
我已经设法通过使用阴影 DOM 元素在 UI 模式下清除 chrome 的浏览历史记录。此方法在用户界面模式下完美运行(即没有无头模式),但我的问题是在无头测试模式下实现它(如果建议的方式也适用于其他浏览器,那就太好了)。对此问题有任何建议或解决方法吗?
public void clearBrowsingHistory() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 15);
driver.get("chrome://settings/clearBrowserData");
// Get shadow root elements
WebElement shadowRoot1 = expandRootElement(driver.findElement(By.xpath("/html/body/settings-ui")));
WebElement root2 = shadowRoot1.findElement(By.cssSelector("settings-main"));
WebElement shadowRoot2 = expandRootElement(root2);
WebElement root3 = shadowRoot2.findElement(By.cssSelector("settings-basic-page"));
WebElement shadowRoot3 = expandRootElement(root3);
WebElement root4 = shadowRoot3
.findElement(By.cssSelector("#advancedPage > settings-section > settings-privacy-page"));
WebElement shadowRoot4 = expandRootElement(root4);
WebElement root5 = shadowRoot4.findElement(By.cssSelector("settings-clear-browsing-data-dialog"));
WebElement shadowRoot5 = expandRootElement(root5);
WebElement root6 = shadowRoot5
.findElement(By.cssSelector("cr-dialog div[slot ='button-container'] #clearBrowsingDataConfirm"));
root6.click();
wait.until(ExpectedConditions.invisibilityOf(root6));
}
解决方案
推荐阅读
- javascript - How do I sum all columns in a table with the same classname in JavaScript?
- r - 如何在 R 中重现这个 Stata 命令?(会议)
- tensorflow - 为什么根据 Tensorflow 文档不鼓励在最后一层使用 softmax 作为激活函数?
- ruby-on-rails - 如何在rails中删除表格的标题?
- c# - 从代码访问 DataTemplate 的元素
- html - 内容被下推,我现在不明白
- javascript - 如何在反应中很好地组织元素(jsx/css)
- ios - “SwiftyStoreKit.completeTransactions() 应该只在应用启动时调用一次。”
- cdi - 完成以编程方式获取的上下文引用后,我应该怎么做?
- windows - 如何找到以下功能和结构的原型