我目前正在尝试学习Python中的硒,我有一个问题,点击"接受所有cookie";按钮。
我正在使用:
Python v3.9
Chrome v87
这是我试图抓取的HTML页面
https://www.currys.co.uk/gbuk/tv -和-家- entertainment/televisions/televisions/samsung ue75tu7020kxxu - 75智能- 4 k - ultra - hd - hdr - led电视- 10213562 - pdt.html
这是我当前的代码
# Selenium Tutorial #1
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome(r"C:UsersSte1337Desktopchromedriverchromedriver.exe")
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
#search = driver.find_element_by_id(ContentPlaceHolder1_NotifyBtn)
driver.implicitly_wait(10)
link = driver.find_element_by_id("onetrust-accept-btn-handler")
link.click
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))
)
element.click
except:
driver.quit()
The "是错过后的点击。试试这个:
link = driver.find_element_by_id("onetrust-accept-btn-handler")
link.click()
等待并点击。
wait = WebDriverWait(driver, 10)
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()