You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium WebDriver无法关闭autotrader.co.uk网站的Cookie弹窗

解决Selenium无法点击Autotrader Cookie弹窗按钮的问题

问题说明

尝试使用Selenium点击autotrader.co.uk网站Cookie弹窗的「全部拒绝」按钮,但弹窗始终无法消失。以下是弹窗的HTML代码和尝试的Python代码:

弹窗HTML代码

<button title="Reject All" aria-label="Reject All" class="message-component message-button no-children focusable sp_choice_type_13" style="opacity: 1; padding: 10px 5px; margin: 10px 5px; border-width: 2px; border-color: rgb(5, 52, 255); border-radius: 5px; border-style: solid; font-size: 14px; font-weight: 400; color: rgb(255, 255, 255); font-family: arial, helvetica, sans-serif; width: calc(35% - 20px); background: rgb(5, 52, 255);">Reject All</button>

原尝试代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
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

path_to_driver = r"C:\path_to_project\chromedriver.exe"
service = Service(executable_path=path_to_driver)

driver = webdriver.Chrome(service=service)
driver.get("https://www.autotrader.co.uk")
time.sleep(5)
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CLASS_NAME, 'message-component message-button no-children focusable sp_choice_type_13'))).click()

time.sleep(10)
driver.quit()

错误原因

  1. 定位方式错误:By.CLASS_NAME仅支持单个类名,传入多个空格分隔的类名会直接导致定位失败。
  2. 冗余的强制等待:time.sleep(5)属于不必要的强制等待,显式等待已经能确保元素加载完成,多余的等待反而会拖慢执行效率。

修正方案

方案1:使用CSS选择器定位(推荐)

利用按钮的title属性直接定位,精准度更高:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

path_to_driver = r"C:\path_to_project\chromedriver.exe"
service = Service(executable_path=path_to_driver)

driver = webdriver.Chrome(service=service)
driver.get("https://www.autotrader.co.uk")

# 通过title属性定位「全部拒绝」按钮
reject_btn = WebDriverWait(driver, 15).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[title="Reject All"]'))
)
reject_btn.click()

driver.quit()

方案2:使用XPATH定位

如果CSS选择器不生效,可尝试XPATH定位:

# 替换定位部分代码
reject_btn = WebDriverWait(driver, 15).until(
    EC.element_to_be_clickable((By.XPATH, '//button[@title="Reject All" and text()="Reject All"]'))
)
reject_btn.click()

额外注意事项

若弹窗嵌套在<iframe>中,需先切换到iframe再定位元素:

# 先切换到弹窗所在的iframe(需根据实际页面HTML确认iframe的定位参数)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "sp_message_iframe")))
# 定位并点击按钮
reject_btn = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[title="Reject All"]')))
reject_btn.click()
# 切换回主文档
driver.switch_to.default_content()

内容的提问来源于stack exchange,提问作者teeeeee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 08:29:51