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

ChromeDriverManager().install()在Jenkins运行时触发网络不可达错误求助

Python BDD + Selenium Jenkins执行网络不可达问题解决

问题定位

报错OSError: [Errno 101] Network is unreachable本质是Jenkins执行环境中,ChromeDriverManager下载驱动或Chrome浏览器访问网络时无法建立外部连接,和本地环境的网络权限、代理配置差异有关。

解决步骤

1. 预安装ChromeDriver,跳过在线下载

Jenkins节点上手动安装对应版本的ChromeDriver,避免在线下载的网络问题:

  • 查看服务器Chrome版本:
google-chrome --version
  • 下载匹配版本的ChromeDriver,放到服务器可执行路径(如/usr/local/bin/)
  • 修改代码,直接指定本地驱动路径:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

self.driver = webdriver.Chrome(
    service=Service("/usr/local/bin/chromedriver"),
    options=chrome_options
)

2. 给ChromeDriverManager显式配置代理

Jenkins的全局代理可能未传递给Python进程,需在代码中设置代理环境变量:

import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# 配置代理(替换为实际代理地址)
os.environ['HTTP_PROXY'] = 'http://proxy-ip:port'
os.environ['HTTPS_PROXY'] = 'http://proxy-ip:port'

self.driver = webdriver.Chrome(
    service=Service(ChromeDriverManager().install()),
    options=chrome_options
)

若代理需认证,格式为http://username:password@proxy-ip:port

3. 检查Jenkins节点的网络连通性

  • 登录Jenkins服务器,执行命令测试ChromeDriver下载源的连通性:
curl https://chromedriver.storage.googleapis.com/
  • 若无法访问,检查服务器防火墙、安全组是否允许出站访问该域名,或联系运维将其加入内网白名单

4. 优化Chrome启动参数(适配Jenkins无桌面环境)

Jenkins通常运行在无桌面环境,需配置Chrome无头模式及必要参数:

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless=new')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# 若目标网站需代理访问,添加Chrome代理参数
chrome_options.add_argument('--proxy-server=http://proxy-ip:port')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 06:41:34