1

I am trying to locate an input text from input field that is saved in shadow DOM with:

driver.find_element_by_css_selector("#my_id div:nth-of-type(2)").text 

but it does not help. HTML part

<input _ngcontent-c21="" class="clr-input ng-untouched ng-pristine ng-valid" id="my_id" name="my_id" placeholder="My placeholder namespace" size="40" type="text">
 #shadow-root {user-agent}
  <div pseudo="-webkit-input-placeholder" id="placeholder" style="display: none !important;">My placeholder namespace</div>
  <div>Text I need to find</div>

Also tried this with not luck:

shadow_section = driver.execute_script('''return 
document.querySelector("#my_id").shadowRoot''')
            print(shadow_section.find_element_by_css_selector("div:nth-of-type(2)").text)

What is the best approach for finding elements in shadow DOM in this case? In my case locator should be linked to element.

Update: as suggested I tried:

  shadow_root = driver.execute_script('return arguments[0].shadowRoot', driver.find_element_by_css_selector("#my_id"))
  shadow_root_element = shadow_root(driver.find_element_by_css_selector('div:nth-child(2)'))

But received:

TypeError: 'NoneType' object is not callable

on shadow_root_element row.

2
  • 1
    You can not query user-agent shadowRoots. Those are Browser specific implementations. So the HTML in Safari could be different from the HTML in Chrome or FireFox. If try to edit it (double-click) in F12 Elements.. you can't. Commented Mar 5, 2021 at 13:13
  • Thank you very much. I understood it in a hard way. Commented Mar 5, 2021 at 21:19

2 Answers 2

1
element=driver.execute_script(
    "return document.querySelector('#my_id').shadowRoot.querySelector('div:nth-child(2)')")

you have to use execute script to find shadow root, the element will have the item you want. You can now use element.text to get the text

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks. When I execute this, I get: selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'querySelector' of null
I found this answer for Java, trying to use it in Python stackoverflow.com/questions/56380091/…
Make sure there is only one element we the id as my id
This id is unique, I checked it in Chrome devtools before. Maybe I should try a different locators for element in shadow DOM.
Find that element and then pass that to execute script and do arguments[0].shadowRoot
|
1

I was not able to find a solution with Javascripts .shadowRoot option and it is a mystery for me why it does not work. This option works just fine. It input text:

driver.find_element_by_css_selector("#my_id").get_attribute("value")

or

driver.find_element_by_id("my_id").get_attribute("value")

I am still very interested in the approach proposed by PDHide.

4 Comments

in the example you showed there is no attribute called value , then how will this work?
Do you have a website link?
The site closed app. I found this answer useful stackoverflow.com/questions/10251525/… . So, in the case of input elements we should use "element.get_attribute('value')". Also, from what I learned it matters what type of #shadow-root it is. In my case it is {user-agent}. I think in other cases javascript approach would work with no problems.
There is no value attribute in my HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.