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

无GUI主机运行Selenium无头爬虫遇AttributeError问题求助

问题分析与解决步骤

核心问题排查

  • 驱动与系统不兼容:你使用的ChromeDriver.exe是Windows平台的可执行文件,cPanel主机属于Linux环境,无法直接运行Windows的exe程序,这是服务启动失败的关键原因。
  • 版本匹配问题:报错AttributeError: 'Service' object has no attribute 'process'常出现在Selenium 4.x版本与旧版ChromeDriver不兼容,或是驱动未正确初始化的场景。
  • 浏览器缺失/路径未指定:cPanel默认环境通常未预装Chrome浏览器,需确认安装并指定正确路径。

具体解决步骤

1. 替换为Linux版本ChromeDriver

登录cPanel终端,先通过google-chrome --version查看浏览器版本,再下载对应版本的Linux驱动:

# 示例:下载ChromeDriver 114.0.5735.90版本(请根据实际版本调整)
wget https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
chmod +x chromedriver

确保解压后的chromedriver文件与代码在同一目录,或在代码中指定绝对路径。

2. 安装Chrome浏览器(cPanel环境)

针对CentOS/RHEL系主机执行以下命令(Debian/Ubuntu系替换为apt-get相关命令):

curl https://intoli.com/install-google-chrome.sh | bash

安装完成后,用which google-chrome获取浏览器路径,后续在代码中指定。

3. 修正Selenium代码配置

调整代码,确保无头模式及路径配置正确:

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

# 配置Chrome无头模式选项
chrome_options = Options()
chrome_options.add_argument("--headless=new")  # Selenium 4.8+推荐的无头模式
chrome_options.add_argument("--no-sandbox")    # 规避cPanel沙箱限制
chrome_options.add_argument("--disable-dev-shm-usage")  # 解决/dev/shm空间不足问题
chrome_options.binary_location = "/usr/bin/google-chrome"  # 替换为实际Chrome路径

# 指定ChromeDriver路径
service = Service(executable_path="./chromedriver")  # 也可使用绝对路径

with webdriver.Chrome(service=service, options=chrome_options) as driver:
    driver.get("https://aqms.doe.ir/App/")
    # 后续爬取逻辑

4. 确保版本兼容

升级Selenium到最新稳定版,避免版本不匹配问题:

pip install --upgrade selenium

额外注意事项

  • 确保所有依赖(Selenium、ChromeDriver)都安装在cPanel创建的Python虚拟环境内,避免全局环境冲突。
  • 检查chromedriver文件权限,通过chmod +x chromedriver确保其可执行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 00:05:26