0

I tried accessing the website to get data but the cookie consent pops up. I tried various ways to click "Accept All." The code always runs perfectly but the button is never clicked. I wonder what is wrong with my code.

This is what I tried

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

driver = webdriver.Chrome()
url = "https://www.aplusa-online.com/vis/v1/en/exhprofiles/JP10FQtPTBydU4Ic9MvR7A?oid=19008&lang=2"
driver.get(url)
time.sleep(5)
element = driver.execute_script("""return document.querySelector('usercentrics-roots').shadowRoot.querySelector("button[data-testid='uc-accept-all-button']")""")
element.click()

2 Answers2

0

You just need to fix the JavaScript query.

Use the below instead:

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

If you notice, the correct CSS selector to locate the div tag with id="usercentrics-root" would be div#usercentrics-root

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
0

The element Accept All is within a #shadow-root (open)

shadowroot


Solution

To click on the element element Accept All you need to induce WebDriverWait for the element_to_be_clickable() and using querySelector() method you can use the following locator strategy:

driver.get("https://www.aplusa-online.com/vis/v1/en/exhprofiles/JP10FQtPTBydU4Ic9MvR7A?oid=19008&lang=2")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((driver.execute_script('''return document.querySelector("div#usercentrics-root").shadowRoot.querySelector("button[data-testid='uc-accept-all-button']")''')))).click()

Note: You have to add the following imports :

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

Browser snapshot:

aplusa-online

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352