Phyton Selenium Shadow-Root can't find element inside

89 views Asked by At

enter image description hereHi can't find or access elements inside a shadow root. How can I check if Im in the correct shadow-root or fix the code?

def getShadowRoot(host):
    shadowRoot = driver.execute_script("return arguments[0].shadowRoot", host)
    return shadowRoot

#enter into shadow root.
shadow = driver.find_element(By.CLASS_NAME, 'proceeds-breakdown-container')
root2 = getShadowRoot(shadow)

#tried to get via list and first element both give empty list or error
posdata = root2.find_elements(By.CLASS_NAME,'second-level-breakdown-amount positive-amount')
if posdata:
    print("printing elements")
    for element in posdata:
        print(element.text)
else:
    #list is always empty
    print("The list is empty.")


#error can't find element
revenue = root2.find_element(By.CLASS_NAME,'second-level-breakdown-amount positive-amount').text

Expecting: Find the element inside the shadow-root

Trying: access several elements via class, xpath, but not working.

1

There are 1 answers

0
MD Kawsar On

You can't directly access The Element of Shadow Root For example try this driver.execute_script() Execute Javascript and then Work with that element! You can also try to detect the element via inspect element>console to make sure the syntax is okay!

Example Code block:

ele = driver.execute_script("return document.querySelector('.proceeds-breakdown-container').shadowRoot.querySelector('.second-level-breakdown-amount positive-amount')")
print(ele.text)