Ubuntu 20.04.3 LTS环境下Python项目启动Selenium ChromeDriver报错求助
Hey there, let's tackle those two issues you're facing with Selenium on Ubuntu 20.04.3 LTS—one's a quick fix for a deprecation warning, the other's the bigger problem of chromedriver exiting unexpectedly.
1. Squash the Deprecation Warning
That <stdin>:1: DeprecationWarning: use options instead of chrome_options message is straightforward: Selenium phased out the chrome_options parameter in favor of options for the Chrome driver constructor. You just need to update that line of code to use the new parameter name.
2. Fix the "Service chromedriver unexpectedly exited" Error
The status code 1 error almost always comes from a version mismatch between Chromium (or Chrome) and chromedriver, or unnecessary setup steps causing conflicts. Let's fix this step by step:
Step 1: Verify Version Compatibility
First, check your installed Chromium version to ensure it matches your chromedriver:
chromium-browser --version
If you installed via apt, the chromium-chromedriver package should already be paired with the correct Chromium version, but double-checking never hurts.
Step 2: Clean Up Your Setup Code
Your current code has redundant steps that might be causing path conflicts. Here's the simplified, working version:
# Update packages and install required tools !apt-get update -y !apt-get install -y chromium-browser chromium-chromedriver # Import Selenium modules from selenium import webdriver from selenium.webdriver.chrome.options import Options # Configure Chrome options for headless mode chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--disable-gpu') # Extra flag for headless mode stability # Initialize the driver (no need to specify chromedriver path—it's already in PATH) wd = webdriver.Chrome(options=chrome_options)
Key Changes Explained:
- Removed manual
cpandsys.pathsteps: Thechromium-chromedriverpackage installs chromedriver to a system PATH location automatically, so you don't need to copy it or add paths manually. - Swapped
chrome_options=chrome_optionsforoptions=chrome_optionsto eliminate the deprecation warning. - Added
--disable-gpu: While not strictly required for Linux headless mode, it can boost stability in some environments.
If the Error Still Happens:
If chromedriver still exits unexpectedly, download the exact chromedriver version matching your Chromium release, extract it, and overwrite the existing one in /usr/bin. Then restart your Python session to apply the change.
内容的提问来源于stack exchange,提问作者PDD

