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?

LJG
  • 601
  • 1
  • 7
  • 15

1 Answers1

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)
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Thank you for your answer. Is it impossible in this case to have a solution with XPath? – LJG Mar 21 '22 at 09:45
  • 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. – cruisepandey Mar 21 '22 at 09:48
  • 1
    @LJG: improved the querySelector above. also Please read https://stackoverflow.com/questions/1063306/xpath-or-queryselector#:~:text=XPath%20is%20much%20faster%20than,%2Fxpath%2Dvs%2Dqueryselectorall why querySelector is preferred over XPath. – cruisepandey Mar 21 '22 at 10:13