在我的测试脚本中,我让selenium打开chrome设置页面。在该页面上,我希望它选择"清除数据"按钮。
driver.FindElement(By.CssSelector("* /deep/ #clearBrowsingDataConfirm"));
我收到一个错误,说找不到元素。我也试过只使用"ID"。还有其他方法可以做到这一点吗。
这是@Lanki的c#版本,但请确保单击"基本"选项卡、"所有时间"时间范围,如果尚未选中,请单击"浏览"、"Cookie"one_answers"缓存"复选框。谢谢@Lanki!
private static IWebElement expandRootElement(IWebElement element) {
return (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].shadowRoot", element);
}
private static void clickSettingsCheckbox(IWebElement chkBox) {
string check = chkBox.GetAttribute("checked");
if (check == null) chkBox.Click();
}
public static void clearCacheAndCookies() {
driver.Navigate().GoToUrl("chrome://settings/clearBrowserData");
driver.SwitchTo().ActiveElement();
IWebElement root1 = driver.FindElement(By.CssSelector("settings-ui"));
IWebElement shadowRoot1 = expandRootElement(root1);
IWebElement root2 = shadowRoot1.FindElement(By.CssSelector("settings-main"));
IWebElement shadowRoot2 = expandRootElement(root2);
IWebElement root3 = shadowRoot2.FindElement(By.CssSelector("settings-basic-page"));
IWebElement shadowRoot3 = expandRootElement(root3);
IWebElement root4 = shadowRoot3.FindElement(By.CssSelector("settings-section > settings-privacy-page"));
IWebElement shadowRoot4 = expandRootElement(root4);
IWebElement root5 = shadowRoot4.FindElement(By.CssSelector("settings-clear-browsing-data-dialog"));
IWebElement shadowRoot5 = expandRootElement(root5);
IWebElement root6 = shadowRoot5.FindElement(By.CssSelector("#clearBrowsingDataDialog"));
IWebElement root7 = root6.FindElement(By.CssSelector("cr-tabs[role='tablist']"));
IWebElement shadowRoot7 = expandRootElement(root7);
IWebElement root8 = shadowRoot7.FindElement(By.CssSelector("div[class='tab selected']"));
IWebElement root9 = shadowRoot7.FindElement(By.CssSelector("div[class='tab']"));
if (!root8.Text.Equals("Basic")) root9.Click(); // Select Basic tab if not selected
IWebElement rootDropDown = root6.FindElement(By.Id("clearFromBasic"));
IWebElement shadowDropDown = expandRootElement(rootDropDown);
IWebElement timeRange = shadowDropDown.FindElement(By.Id("dropdownMenu"));
var selectElement = new SelectElement(timeRange);
selectElement.SelectByText("All time");
clickSettingsCheckbox(root6.FindElement(By.Id("browsingCheckboxBasic")));
clickSettingsCheckbox(root6.FindElement(By.Id("cookiesCheckboxBasic")));
clickSettingsCheckbox(root6.FindElement(By.Id("cacheCheckboxBasic")));
IWebElement clearDataButton = root6.FindElement(By.CssSelector("#clearBrowsingDataConfirm"));
clearDataButton.Click();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(wd => wd.Url.StartsWith("chrome://settings"));
Thread.Sleep(300);
return;
}
那个按钮真的很难够到,因为是聚合物。试试这个,它对我有用。
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace Selenium_test
{
class Program
{
static IWebDriver driver;
static void Main(string[] args)
{
driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
driver.Navigate().GoToUrl("chrome://settings/clearBrowserData");
IWebElement root1 = driver.FindElement(By.CssSelector("settings-ui"));
IWebElement shadowRoot1 = expandRootElement(root1);
IWebElement root2 = shadowRoot1.FindElement(By.CssSelector("settings-main"));
IWebElement shadowRoot2 = expandRootElement(root2);
IWebElement root3 = shadowRoot2.FindElement(By.CssSelector("settings-basic-page"));
IWebElement shadowRoot3 = expandRootElement(root3);
IWebElement root4 = shadowRoot3.FindElement(By.CssSelector("settings-section > settings-privacy-page"));
IWebElement shadowRoot4 = expandRootElement(root4);
IWebElement root5 = shadowRoot4.FindElement(By.CssSelector("settings-clear-browsing-data-dialog"));
IWebElement shadowRoot5 = expandRootElement(root5);
IWebElement root6 = shadowRoot5.FindElement(By.CssSelector("#clearBrowsingDataDialog"));
IWebElement root7 = root6.FindElement(By.CssSelector("cr-tabs[role='tablist']"));
root7.Click();
IWebElement clearDataButton = root6.FindElement(By.CssSelector("#clearBrowsingDataConfirm"));
clearDataButton.Click();
}
private static IWebElement expandRootElement(IWebElement element)
{
return (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("return arguments[0].shadowRoot", element);
}
}
}
因为我是个新手,所以我无法投票支持@Lanki的回答。它就像一个符咒。
以下是相同脚本的JAVA版本:
WebElement root1 = driver.findElement(By.cssSelector("settings-ui"));
WebElement shadowRoot1 = expandRootElement(root1);
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("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("#clearBrowsingDataDialog"));
WebElement root7 = root6.findElement(By.cssSelector("cr-tabs[role='tablist']"));
root7.click();
WebElement clearDataButton = root6.findElement(By.cssSelector("#clearBrowsingDataConfirm"));
clearDataButton.click();