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

如何用Selenium Python关闭Oddschecker网站的Cookie及广告弹窗

解决Oddschecker弹窗定位与关闭问题

一、锁定弹窗的技巧

遇到点击检查弹窗就消失的情况,用这招锁定弹窗:

  • 按F12打开Chrome开发者工具,切换到Sources标签页
  • 弹窗出现瞬间,点击DevTools左上角的暂停按钮(⏸️),或者在控制台输入debugger;回车,页面会暂停,此时弹窗不会消失,就能正常查看元素标签了

二、关闭Cookie弹窗的代码

结合WebDriverWait等待元素可点击,示例代码如下:

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

# 初始化Chrome驱动
s = Service(r'C:\Program Files (x86)\chromedriver.exe')
driver = webdriver.Chrome(service=s)
driver.get('https://www.oddschecker.com/')

# 等待并关闭Cookie弹窗
try:
    # 等待同意/拒绝按钮可点击,XPath可根据实际页面调整
    cookie_btn = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Accept All') or contains(text(), 'Reject All')]"))
    )
    cookie_btn.click()
except Exception as e:
    print("Cookie弹窗未出现或定位失败:", e)

三、关闭广告弹窗的代码

广告弹窗一般有"Close"或"×"按钮,同样用等待点击的方式:

# 等待并关闭广告弹窗
try:
    # CSS选择器可根据实际元素调整
    ad_close_btn = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid='close-button'], .close-icon"))
    )
    ad_close_btn.click()
except Exception as e:
    print("广告弹窗未出现或定位失败:", e)

四、注意事项

  • 网站元素可能会更新,如果定位失效,重新用暂停方法获取最新的XPath或CSS选择器
  • 确保ChromeDriver版本和本地Chrome浏览器版本一致,避免兼容性问题
  • 网络加载慢时,可以把等待时间(代码里的10)改成15,提高成功率

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 07:50:30