Can't find shadow DOM element (JAVA)

386 views
Skip to first unread message

Alexandr Kalugin

unread,
Apr 8, 2016, 12:00:20 AM4/8/16
to Selenium Users
Can't find shadow DOM element (Selenium, JAVA)
I find, how do this in JavaScript, but it's doesn't work in Java. (/deep/) (::shadow)
this is example of DOM

if i use in css selector "#email #tinner-editor" i get exception: NoSuchElement no such element

if i use <input> to sendKeys some text, i get exception:

 org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=49.0.2623.87) (Driver info: chromedriver=2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b),platform=Mac OS X 10.10.5 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 12 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html Build info: version: '2.48.2', revision: '41bccdd10cf2c0560f637404c2d96164b67d9d67', time: '2015-10-09 13:08:06' System info: host: 'MacBook-Pro-Aleksandr.local', ip: '192.168.1.41', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.10.5', java.version: '1.8.0_45' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={userDataDir=/var/folders/jr/z171q85n42n80hynjhgcd8pr0000gn/T/.org.chromium.Chromium.NDUOve}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=49.0.2623.87, platform=MAC, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 7945e93de6e00a67b15be43e58de88d2 at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:326) at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:51) at com.sun.proxy.$Proxy7.click(Unknown Source) at ru.csssr.hr.pages.OrderListPage.loginOrderPage(OrderListPage.java:79) at tests.AddOrderTest.createOrder(AddOrderTest.java:46) 
... Removed 34 stack frames 

Prahlad Boyina

unread,
Apr 9, 2019, 3:24:16 AM4/9/19
to Selenium Users
Can you find the solution to read the shadow element , got same issue for me . !

-Prahald

Shiva Prasad Adirala

unread,
Apr 9, 2019, 3:48:00 AM4/9/19
to Selenium Users

Mike Hetzer

unread,
Apr 9, 2019, 3:55:49 PM4/9/19
to Selenium Users
Try this code below - this in C# FYI.

So, from my limited interaction with shadow dom, I have found this to work.
First you have to locate the shadow dom element and then "expand" it.
See my ExpandRootElement extension method will do this and return the IWebElement via the JS executor.
You can likely stick this on your BasePage as well.

After that, you must find the element you need under the shadow dom stemming from the shadow dom element you just found.
Couple caveats here - when locating an element from this point, you can only use the "id" or "css" selectors - other ones will not work.

Furthermore the /deep css selector has been deprecated.



                public void PageObjectMethodForElementUnderShadowHost()
{
// Finding and expanding the shadow dom "root"
IWebElement shadowRoot = driver.FindElement(By.TagName("")).ExpandRootElement(driver);
By elementUnderShadowSelector = By.Id("inner-editor");

// Finding element under shadow
IWebElement elementIWant =  shadowRoot.FindElement(elementUnderShadowSelector);
}

                public static IWebElement ExpandRootElement(this IWebElement element, IWebDriver driver)
{
return (IWebElement)((IJavaScriptExecutor)driver)
.ExecuteScript("return arguments[0].shadowRoot", element);
}

 
Reply all
Reply to author
Forward
0 new messages