1

My goal is to disable cookies when I access page https://www.icribis.com/it/ (that is click on the button "Rifiuta"). My code, which is not working, is:

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
import time

url = 'https://www.icribis.com/it/'

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)

time.sleep(5)

wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="uc-center-container"]/div[2]/div/div[1]/div/div[2]/button[2]'))).click()

time.sleep(5)

driver.close()

I found the XPath by inspecting the element on the web page.

How can I correct it?

1 Answer 1

1

It's in shadow-root.

You will have to use execute_script

url = 'https://www.icribis.com/it/'

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)

time.sleep(5)

cookie_dsbl_btn = driver.execute_script('return  document.querySelector("#usercentrics-root").shadowRoot.querySelector("#uc-center-container > div:nth-child(2) div > button:nth-child(3)")')
cookie_dsbl_btn.click()

time.sleep(5)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. Is it impossible in this case to have a solution with XPath?
I am not 100% sure if we can have XPath solution, I generally copy JS path from the HTMLDOM to deal with shadow-root. However I do agree we can have bit more better querySelector.
@LJG: improved the querySelector above. also Please read stackoverflow.com/questions/1063306/… why querySelector is preferred over XPath.

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.