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

Python使用Undetected Chromedriver v2时显式等待不生效如何解决

问题修复方案

根因定位

  • 配置参数写法错误:你将add_experimental_option相关配置当成启动参数传入add_argument方法,导致Undetected Chromedriver(以下简称UC)驱动实例初始化异常,Selenium原生元素查询接口被阻塞,每次轮询检测元素状态都无响应,直到等待时长耗尽才返回结果。
  • UC反检测补丁和Selenium原生显式等待逻辑冲突:UC v2默认开启CDP拦截做反检测适配,原生WebDriverWait调用的元素可见性检测接口会被补丁拦截阻塞,无法实时获取元素状态。
  • 可见性检测规则适配问题:你使用的visibility_of_element_located需要同时校验DOM存在、元素可见、文本内容匹配三个条件,UC加载页面时会注入反检测缓存,元素属性查询请求会被临时拦截,导致检测逻辑始终不满足触发条件。

修复步骤

  1. 修正Options配置错误,移除arguments列表中错误的实验性配置项,单独调用对应方法设置:
    • 删除arguments列表中的'--add_experimental_option("detach", True)'、'--credentials_enable_service=False'、'--profile.password_manager_enabled=False,'三个错误参数
    • 单独添加实验性配置:
    options.add_experimental_option("detach", True)
    options.add_experimental_option("credentials_enable_service", False)
    options.add_experimental_option("profile.password_manager_enabled", False)
    
  2. 替换显式等待逻辑,优先使用UC自带的等待方法,兼容性更好:
    直接调用UC内置的wait_for_element方法,适配了自身的反检测补丁,不会出现阻塞问题。如果要保留原生WebDriverWait,可缩短轮询间隔到0.1s,同时优先检测元素存在性再判断可见性。
  3. 添加UC推荐的防阻塞启动参数:
    在arguments列表中加入'--no-sandbox'、'--disable-dev-shm-usage'两个参数,减少驱动实例阻塞概率。

修复后参考代码

import undetected_chromedriver as uc
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

class My_Chrome(uc.Chrome):
    def __del__(self):
        pass

options = uc.ChromeOptions()
arguments = [
    '--log-level=3', '--no-first-run', '--no-service-autorun', '--password-store=basic',
    '--start-maximized',
    '--window-size=1920,1080',
    '--no-sandbox',
    '--disable-dev-shm-usage'
]
for argument in arguments:
    options.add_argument(argument)

# 单独添加实验性配置
options.add_experimental_option("detach", True)
options.add_experimental_option("credentials_enable_service", False)
options.add_experimental_option("profile.password_manager_enabled", False)

driver = My_Chrome(options=options)
# 可选:使用UC自带等待方法,替换原生WebDriverWait
# wait = WebDriverWait(driver, 20, poll_frequency=0.1)

driver.get('https://www.oddschecker.com')

try:
    # 用UC自带方法等待元素,兼容性更好
    opt_in = driver.wait_for_element("//span[text()='Not Now']/..", timeout=20)
    # 原生写法替换方案:先检测存在再判断可见
    # opt_in = wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Not Now']/..")))
    # wait.until(EC.visibility_of(opt_in))
    VirtualClick(driver, opt_in)
    current_time('Closing opt-in')
except:
    pass

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 00:45:02