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

Python Selenium搭配无头ChromeDriver启动失败:DevTools请求出错

Hey there, let's work through this issue together—since you had the script running smoothly on your MacBook Pro but hit snags moving it to Ubuntu 16.04 Server (Python 2.7.12), here are the most likely fixes to get your Selenium setup working again:

Common Troubleshooting Steps

1. Fix ChromeDriver Path Configuration

Ubuntu Server might not have ChromeDriver added to your system PATH, so Python can't locate it automatically. Try explicitly defining the full path in your code:

from selenium import webdriver

# Replace with your actual ChromeDriver path (find it via `which chromedriver` in terminal)
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')

2. Enable Headless Mode (Critical for Server Environments)

Ubuntu Server has no graphical interface, so Chrome needs to run in headless mode. The GPU error you saw can also be suppressed with a dedicated flag. Update your code to include these essential arguments:

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

chrome_options = Options()
chrome_options.add_argument('--headless')  # Run without GUI
chrome_options.add_argument('--no-sandbox')  # Bypass security restrictions (required for server users)
chrome_options.add_argument('--disable-dev-shm-usage')  # Fixes /dev/shm space limitations
chrome_options.add_argument('--disable-gpu')  # Suppresses the GPU error you mentioned

driver = webdriver.Chrome(options=chrome_options)

3. Resolve Python 2.7 & Selenium Compatibility

Latest Selenium versions drop support for Python 2.7—this is probably the root cause of your failure. Roll back to the last Selenium version that works with Python 2.7:

pip uninstall selenium
pip install selenium==3.141.0

Also double-check that your ChromeDriver version matches your installed Chrome version (run google-chrome --version to confirm). Selenium 3.141.0 supports ChromeDriver versions 2.40 through 79; download a matching driver if needed.

4. Install Missing System Dependencies

Ubuntu Server often lacks graphical libraries Chrome needs to run. Install these core dependencies first:

sudo apt-get install -y libxss1 libappindicator1 libindicator7

If issues persist, add a virtual display with xvfb to simulate a GUI environment:

sudo apt-get install -y xvfb
pip install pyvirtualdisplay

Then update your code to initialize the virtual display before launching Chrome:

from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Start virtual display
display = Display(visible=0, size=(800, 600))
display.start()

# Configure Chrome options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=chrome_options)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:07:04