Selenium Webdriver with Shadow DOM

独自空忆成欢 提交于 2019-12-04 19:43:41

Try to locate your element like this:

driver.FindElement(By.CssSelector('selector_otside_shadow_root /deep/ selector_inside_shadow_root')); 

in your case it would be:

driver.FindElement(By.CssSelector('app-home /deep/ #itemName1'));

You can test this approach in chrome://downloads/ link with this css_selector:

downloads-manager /deep/ downloads-item /deep/ [id=file-link]

in dev tools. As you can see there was needed to pass two shadow-root elements, so make sure that you have only one shadow-root element or use multiple /deep/ like in example above.

or you can use JavasciptExecutor like this:

IJavaScriptExecutor js = (IJavaScriptExecutor)_driver;
var element = js.ExecuteScript("return document.querySelector('selector_outside_shadow_root').shadowRoot.querySelector('selector_inside_shadow_root');");
  • Note: the first suggestion as far I know works only in Chrome, if you want a cross browser solution - use the second one.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!