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

Ubuntu 20.04.3服务器上Selenium Python脚本运行失败求助

解决Ubuntu 20.04 Jenkins环境下Selenium Python脚本运行失败问题

核心问题排查

你的脚本本地能正常运行但服务器端失败,大概率是Ubuntu服务器上未安装Chrome浏览器本体——webdriver-manager仅负责匹配安装ChromeDriver驱动,不会自动安装Chrome浏览器程序。

具体修复步骤

1. 在Ubuntu Jenkins节点安装Chrome浏览器

执行以下命令完成安装:

# 更新系统包列表
sudo apt update

# 安装必要依赖
sudo apt install -y wget gnupg2 apt-transport-https

# 添加Chrome官方软件源
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" | sudo tee /etc/apt/sources.list.d/google-chrome.list

# 安装Chrome稳定版
sudo apt update && sudo apt install -y google-chrome-stable

2. 验证浏览器安装状态

执行命令确认Chrome已成功安装:

google-chrome --version

3. 优化脚本参数(可选但推荐)

在现有配置基础上,补充适配服务器环境的参数,提升稳定性:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
options.add_argument('--headless=new')  # 启用新版无头模式,兼容性更强
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')  # 服务器环境无需GPU加速
options.add_argument('--window-size=1920,1080')  # 固定窗口尺寸,避免页面布局异常

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
 
driver.get("https://python.org")
print(driver.title)
driver.quit()  # 用quit()替代close(),确保彻底释放浏览器资源

4. 处理Jenkins权限问题(若出现)

如果遇到权限报错,确保Jenkins用户拥有Chrome和ChromeDriver的执行权限:

sudo chmod +x /usr/bin/google-chrome
sudo chmod +x $(which chromedriver)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 13:57:09