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

如何将Selenium Driver转换为SeleniumBase CDP Driver?报错求助

解决Selenium Driver转SeleniumBase CDP Driver的AttributeError问题

问题场景

现有代码先启动标准Selenium Chrome Driver,尝试转换为SeleniumBase CDP Driver时触发报错:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opts = Options()
opts.add_argument(f"--proxy-server={a_proxy}")
opts.add_argument("--disable-blink-features=AutomationControlled")
opts.add_experimental_option('useAutomationExtension', False)
opts.add_experimental_option("excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(options=opts)

loop = asyncio.new_event_loop()
cdp_driver = loop.run_until_complete(cdp_util.create_from_driver(driver))
url = "www.planetminecraft.com/account/sign_in/"
page = loop.run_until_complete(cdp_driver.get(url))
sb = CDPMethods(loop, page, cdp_driver)

报错信息:

Traceback (most recent call last):
  File "C:\Users\Administrator\PycharmProjects\selenium_base_example\main.py", line 58, in <module>
    cdp_driver = loop.run_until_complete(cdp_util.create_from_driver(driver))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\seleniumbase\undetected\cdp_driver\cdp_util.py", line 488, in create_from_driver
    host, port = driver.options.debugger_address.split(":")
                 ^^^^^^^^^^^^^^
AttributeError: 'WebDriver' object has no attribute 'options'

错误原因

  1. SeleniumBase的cdp_util.create_from_driver方法依赖传入的driver实例具备options属性,且该属性包含debugger_address,但标准Selenium的webdriver.Chrome实例默认不暴露options属性。
  2. 启动Chrome时未开启远程调试端口,即便有options属性也无法获取CDP连接所需的调试地址。

解决思路与修改代码

核心修改点

  • 启动Chrome时强制开启远程调试端口,这是CDP协议通信的必要条件。
  • 手动为标准WebDriver实例添加options属性,适配SeleniumBase的方法要求。

修改后的完整代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import asyncio
from seleniumbase.undetected.cdp_driver.cdp_util import create_from_driver
from seleniumbase.undetected.cdp_driver.cdp_methods import CDPMethods

# 替换为实际代理地址
a_proxy = "http://your-proxy-ip:port"

# 配置Chrome选项
opts = Options()
opts.add_argument(f"--proxy-server={a_proxy}")
opts.add_argument("--disable-blink-features=AutomationControlled")
opts.add_experimental_option('useAutomationExtension', False)
opts.add_experimental_option("excludeSwitches", ["enable-automation"])
# 开启远程调试端口,指定固定端口便于CDP连接
opts.add_argument("--remote-debugging-port=9222")
opts.add_argument("--remote-debugging-address=127.0.0.1")

# 启动标准Selenium Chrome Driver
driver = webdriver.Chrome(options=opts)

# 手动为driver添加options属性,满足cdp_util的读取要求
driver.options = opts

# 创建CDP Driver并执行后续操作
loop = asyncio.new_event_loop()
cdp_driver = loop.run_until_complete(create_from_driver(driver))

url = "https://www.planetminecraft.com/account/sign_in/"
page = loop.run_until_complete(cdp_driver.get(url))
sb = CDPMethods(loop, page, cdp_driver)

替代方案(无需修改driver实例)

如果不想给driver实例添加属性,可直接调用create_cdp_driver方法,手动传入调试地址:

# 替换create_from_driver的调用代码
cdp_driver = loop.run_until_complete(create_cdp_driver("127.0.0.1:9222"))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 05:40:01