1

I am trying to click the Accept button on the pop-up for cookies. Here's the code that I have tried:

driver.get(r'https://www.studydrive.net/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.sc-gtsrHT.iETHdM"))).click()

Here's the error:

raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

I have also tried using X-path but was not able to click on the button. Any help is highly appreeciated.

1 Answers1

0

This pop-up is on the shadow dom.
Selenium does not provide explicit support to work with Shadow DOM elements, as they are not in the current dom. That's the reason why we will get NoSuchElementException exception when try to access the elements in the `shadow dom.
With the following JavaScript this should work:

driver.get(r'https://www.studydrive.net/')
time.sleep(5)
accept_all_btn = driver.execute_script('''return document.querySelector('#usercentrics-root').shadowRoot.querySelector('button[aria-label="Accept All"]')''')
accept_all_btn.click()

See more explanations here and here

Prophet
  • 32,350
  • 22
  • 54
  • 79