Windows10 Bash环境下Chromedriver在PATH中但Selenium报错求助
Hey there, let's work through this ChromeDriver problem you're hitting in the Windows Bash Shell (I’m assuming this is WSL, right?). The key thing to remember here is that the Bash environment doesn’t share Windows' PATH directly, and there are a few other gotchas with Python 2.7 and cross-environment tooling. Here's what to try:
Don’t rely on Windows' PATH for WSL Bash
When you added ChromeDriver to Windows' PATH, that only affects Windows apps—your Bash shell has its own separate environment variables. To fix this:- First, find the WSL equivalent path of your ChromeDriver. If it’s in
C:\tools\chromedriver.exe, in Bash that becomes/mnt/c/tools/chromedriver.exe. - Either update your Python code to use this WSL-style path explicitly:
from selenium import webdriver driver = webdriver.Chrome(executable_path='/mnt/c/tools/chromedriver.exe') - Or add the path to your Bash
PATHvariable permanently:- Open
~/.bashrcin a text editor (likenano ~/.bashrc) - Add this line at the end:
export PATH="$PATH:/mnt/c/tools"(replace with your actual folder path) - Save and run
source ~/.bashrcto apply the changes immediately.
- Open
- First, find the WSL equivalent path of your ChromeDriver. If it’s in
Verify ChromeDriver-Chrome Version Compatibility
This is one of the most common causes of errors. ChromeDriver must match the major version of your installed Chrome browser. For example, if you’re running Chrome 118.x, you need ChromeDriver 118.x—using an older or newer version will throw errors. Double-check this first!Check Your Selenium Version for Python 2.7
Python 2.7 is end-of-life, so the latest Selenium versions don’t support it. Make sure you’re using a compatible Selenium release—Selenium 3.x is the last major version that works with Python 2.7. You can install it with:pip install selenium==3.141.0Test ChromeDriver Directly in Bash
Before blaming your Python code, test if ChromeDriver runs at all in the Bash shell. Run the full path to the executable:/mnt/c/tools/chromedriver.exe --versionIf this throws an error, you might have a WSL compatibility issue. For WSL2, you may need to ensure Windows Subsystem for Linux is updated, or try running Chrome in headless mode in your Python code (since WSL doesn’t have a GUI by default):
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.add_argument("--no-sandbox") driver = webdriver.Chrome(executable_path='/mnt/c/tools/chromedriver.exe', options=chrome_options)
Give these steps a try—most likely the PATH mismatch between Windows and WSL is the main culprit, but checking version compatibility never hurts!
内容的提问来源于stack exchange,提问作者sCuper

