0

I am trying to interact with a web page with a script in python Selenium. Now I found a website with shadow-root. As founded on the web, I tried to use:

shadow_host1 = driver.find_element(By.CSS_SELECTOR, "[id ='bt-inner-page']")
shadow_root1 = driver.execute_script('return arguments[0].shadowRoot', shadow_host1)
elem = shadow_root1.find_element(By.CSS_SELECTOR, ".bt72 > svg:nth-child(1)")

The elem is the search button (see picture, red box). The website is "https://www.tg.casino/en/sports"

I cannot found the elem, the goal is to click on it.

Actually I am using Firefox as browser, seems that the access to shadow root is made (i don't have errors) but I am not capable to locate element (tried with XPATH, CSS_SELECTOR, CLASS_SELECTOR)

1 Answer 1

0

You don't wait for shadow DOM to be rendered, so you have this issue, when actually you have your element in partially rendered state.

To wait for rendering you should:

  • wait for shadow host visbility
  • get it's internal root
  • wait for you element to be rendered inside shadow DOM

Example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
timeout = 10
wait = WebDriverWait(driver, timeout)

def get_shadow_root(element):
    return driver.execute_script('return arguments[0].shadowRoot', element)

driver.get("https://www.tg.casino/en/sports")
driver.maximize_window()

shadow_host = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[id='bt-inner-page']")))
shadow_container = get_shadow_root(shadow_host)
shadow_container_root = shadow_container.find_element(By.ID, 'bt-root')
elem = WebDriverWait(shadow_container_root, timeout).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.bt72')))
elem.click()
Sign up to request clarification or add additional context in comments.

Comments

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.