【发布时间】:2019-10-16 05:14:32
【问题描述】:
我一直在关注How to automate shadow DOM elements using selenium? 的讨论以使用#shadow-root (open) 元素。
在清除浏览数据弹出窗口中定位清除数据按钮的过程中,通过Selenium访问urlchrome://settings/clearBrowserData时出现em> 我找不到以下元素:
#shadow-root (open)
<settings-privacy-page>
快照:
使用Selenium 以下是我的代码试验和遇到的相关错误:
-
尝试 1:
WebElement root5 = shadow_root4.findElement(By.tagName("settings-privacy-page"));-
错误:
Exception in thread "main" org.openqa.selenium.JavascriptException: javascript error: b.getElementsByTagName is not a function
-
-
尝试 2:
WebElement root5 = shadow_root4.findElement(By.cssSelector("settings-privacy-page"));-
错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"settings-privacy-page"}
-
-
尝试 3:
WebElement root5 = (WebElement)((JavascriptExecutor)shadow_root4).executeScript("return document.getElementsByTagName('settings-privacy-page')[0]");-
错误:
Exception in thread "main" java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to org.openqa.selenium.JavascriptExecutor
-
如果有帮助,初始代码块(直到上面的行)可以完美运行:
driver.get("chrome://settings/clearBrowserData");
WebElement root1 = driver.findElement(By.tagName("settings-ui"));
WebElement shadow_root1 = expand_shadow_element(root1);
WebElement root2 = shadow_root1.findElement(By.cssSelector("settings-main#main"));
WebElement shadow_root2 = expand_shadow_element(root2);
WebElement root3 = shadow_root2.findElement(By.cssSelector("settings-basic-page[role='main']"));
WebElement shadow_root3 = expand_shadow_element(root3);
WebElement root4 = shadow_root3.findElement(By.cssSelector("settings-section[page-title='Privacy and security']"));
WebElement shadow_root4 = expand_shadow_element(root4);
PS:expand_shadow_element() 完美无瑕。
【问题讨论】:
-
我想知道,如果这是一个单独的子窗口,因为它在前台,这就是为什么无法在屏幕上找到 uielement 的原因。拿到子窗口句柄后,应该可以抓取uielement了吧?
-
清除数据不属于任何子窗口,chrome 已实现
Shadow DOM作为此增强功能的一部分(不记得发生此更改时的确切版本)。检查我的答案,这将使您更有洞察力。 -
查看更简洁的实现here
标签: java css selenium css-selectors shadow-dom