Home > Software engineering >  Selenium - unable to locate button on cookie popup
Selenium - unable to locate button on cookie popup

Time:12-21

I am trying to parse a website using selenium. The website is here: enter image description here

So in order to be able to interact with the button we must first select the parent of the shadow root, which is the div element with id=usercentrics-root, and then we can select the button and click it:

driver.execute_script('''return document.querySelector("#usercentrics-root").shadowRoot.querySelector("button[data-testid='uc-accept-all-button']")''').click()

Alternatively, a more human readable code:

shadow_parent = driver.find_element(By.CSS_SELECTOR, '#usercentrics-root')
outer = driver.execute_script('return arguments[0].shadowRoot', shadow_parent)
outer.find_element(By.CSS_SELECTOR, "button[data-testid='uc-accept-all-button']").click()
  • Related