Webdriver RecursionError排查:Service初始化及requests调用异常
网页抓取项目WebDriver初始化触发递归深度超限错误
我正在做一个网页抓取项目,初始化WebDriver的代码如下:
import logging import requests from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service from RPA.FileSystem import FileSystem class CustomSelenium: def __init__(self) -> None: self.driver = None self.file = FileSystem() def set_chrome_options(self): options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument("--disable-extensions") options.add_argument("--disable-gpu") options.add_argument('--disable-web-security') options.add_argument("--start-maximized") options.add_argument('--remote-debugging-port=9222') options.add_experimental_option("excludeSwitches", ["enable-logging"]) return options def set_webdriver(self): # options = self.set_chrome_options() # service = Service(ChromeDriverManager().install()) # self.driver = webdriver.Chrome(service=service, options=options) options = webdriver.ChromeOptions() service = Service(ChromeDriverManager().install()) self.driver = webdriver.Chrome() print("WebDriver initialized successfully.") my_driver = CustomSelenium() my_driver.set_webdriver()
这段代码之前运行正常,但现在触发了递归错误:
[Previous line repeated 488 more times] RecursionError: maximum recursion depth exceeded while calling a Python object
我做了测试,把代码里和Service相关的部分注释掉后,错误就消失了:
def set_webdriver(self): # options = self.set_chrome_options() # service = Service(ChromeDriverManager().install()) # self.driver = webdriver.Chrome(service=service, options=options) options = webdriver.ChromeOptions() # service = Service(ChromeDriverManager().install()) self.driver = webdriver.Chrome() print("WebDriver initialized successfully.")
但我不确定移除Service会不会有什么潜在问题,另外发现调用response = requests.get(url)时也会触发一模一样的错误,求帮忙解决。
我的依赖版本如下:
dependencies: - python=3.10.12 - pip=23.2.1 - robocorp-truststore=0.8.0 - nodejs=16.20.2 - pip: - robotframework-browser==17.5.2
问题分析与解决思路
1. 递归错误的核心原因
从现象来看,递归溢出同时出现在Service初始化和requests.get调用时,大概率是**robocorp-truststore包的钩子函数与当前依赖版本出现了递归调用冲突**。这个包会自动修改Python的SSL上下文,若和selenium/requests的底层实现触发循环调用,就会导致递归深度超限。
2. 关于移除Service的影响
- 移除
Service后,Selenium会尝试自动查找系统中的ChromeDriver,但如果环境里Chrome版本和Driver版本不匹配,会直接启动失败。 - 保留
Service + ChromeDriverManager的作用是自动下载匹配当前Chrome版本的Driver,避免版本不兼容问题,所以不建议长期移除。
3. 具体修复步骤
方案一:调整robocorp-truststore版本
尝试降级或升级该包,比如降到0.7.0,或者升级到最新稳定版,看是否能解决递归冲突:
pip install robocorp-truststore==0.7.0
方案二:禁用robocorp-truststore的自动钩子
在代码最开头添加以下代码,强制关闭该包的SSL钩子,避免干扰:
import os os.environ["ROBOCORP_TRUSTSTORE_DISABLE_AUTO_INSTALL"] = "1"
方案三:隔离依赖环境
如果是依赖包之间的版本冲突,建议用虚拟环境重新安装依赖,只保留项目必需的包,避免不必要的依赖干扰:
- 创建并激活虚拟环境
- 只安装
selenium、webdriver-manager、requests和项目必需的RPA包,再测试运行。
4. 验证requests问题
修复后,测试requests.get(url)是否还触发错误,如果仍有问题,可手动指定SSL上下文绕过robocorp-truststore的干预:
import ssl import requests ctx = ssl.create_default_context() response = requests.get(url, verify=ctx)
内容的提问来源于stack exchange,提问作者fatbee7
相关产品推荐
相关产品推荐

