简体   繁体   中英

I can't click a button with Selenium Python

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM