Reputation: 794
I have the following typical shadow dom html code:
<input is="text-input" class="input-element style-scope paper-input"
autocomplete="off" placeholder="" autocapitalize="none" autocorrect="off"
aria-describedby="" aria-labelledby="paper-input-label-22" id="input-22"
tabindex="0">
#shadow-root (user-agent)
<div id="inner-editor">test</div>
</input>
I am using Selenium to try to get the text inside the of the shadow root, but it always returns null, I know the differences between open vs closed shadow DOM, and the open version of Shadow dom always displays as #shadow-root (open) in my Devtool, so I am wondering how can I tell if this user-agent shadow dom is open or closed?
Edit: I am using JS and my code look like this:
driver.executeScript("return
arguments[0].shadowRoot",driver.findElement(webdriver.By.css("#input-
22"))).then(function(text) { text.getText()});
text come back as null.
Upvotes: 4
Views: 2415
Reputation: 17531
You can tell by seeing if the innerHTML
of the root element is empty or not, see this example
import selenium
from selenium import webdriver
driver = webdriver.Chrome()
from bs4 import BeautifulSoup
def expand_shadow_element(element):
shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
return shadow_root
driver.get("chrome://settings")
root1 = driver.find_element_by_tag_name('settings-ui')
html_of_interest=driver.execute_script('return arguments[0].innerHTML',root1)
sel_soup=BeautifulSoup(html_of_interest, 'html.parser')
sel_soup# empty root not expande
shadow_root1 = expand_shadow_element(root1)
html_of_interest=driver.execute_script('return arguments[0].innerHTML',shadow_root1)
sel_soup=BeautifulSoup(html_of_interest, 'html.parser')
sel_soup
Upvotes: 1
Reputation: 794
As per @FlorentB. suggests,
driver.findElement(By.css("#input-22")).getAttribute("value")
will return the text value of the user agent shadow root.
Upvotes: 4