Home > Back-end >  I can't click a button with Selenium Python
I can't click a button with Selenium Python

Time:03-21

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?

CodePudding user response:

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)
  • Related