Debian 10实例中Python脚本使用undetected-chromedriver v2连接Google Chrome失败的技术求助
Hey there, let's work through your undetected-chromedriver issue step by step—there are a few clear culprits in your setup and logs that we can address:
1. Fix the Chrome Command Line Argument Format
This is likely the primary cause of your connection failure. You're passing multiple Chrome flags in a single add_argument call, which Chrome will interpret as one invalid argument instead of three separate ones:
# ❌ Wrong way (combines multiple flags into one) options.add_argument('--no-first-run --no-service-autorun --password-store=basic') # ✅ Correct way (each flag gets its own add_argument call) options.add_argument('--no-first-run') options.add_argument('--no-service-autorun') options.add_argument('--password-store=basic')
When Chrome can't parse these flags correctly, it fails to start properly—hence the "chrome not reachable" error.
2. Resolve Chromedriver Version Conflicts
Looking at your logs, undetected-chromedriver is automatically downloading Chromedriver 92.0.4515.107, but you've installed Chrome 93.0.4577.63 and Chromedriver 93.0.4577.15. Even though you specified executable_path, the library is overriding this with its own downloaded (and outdated) driver.
Fix this by disabling auto-install to force it to use your local, matching version:
driver = uc.Chrome( executable_path=CHROME_DRIVER_PATH, options=options, auto_install=False # Tell it to skip downloading a new driver )
Make sure your local Chromedriver is correctly patched by undetected-chromedriver (it should handle this automatically when you first run it with auto_install=False if the driver is in the specified path).
3. Fix Xvfb Virtual Display Conflicts
Your logs show _XSERVTransMakeAllCOTSServerListeners: server already running—this means a previous Xvfb process didn't shut down properly and is hogging the display port.
First, kill any leftover Xvfb processes:
pkill -9 Xvfb
If you're manually starting pyvirtualdisplay, specify an unused display number to avoid conflicts:
from pyvirtualdisplay import Display display = Display(visible=0, size=(1280, 1024), display_num=2) # Use a less common port like 2 display.start()
Note: If you're using undetected-chromedriver's built-in headless mode (your log says Loading UNDETECTED chromedriver (headless,even)...), you might not need to manually start pyvirtualdisplay at all—the library handles this internally.
4. Verify User Data Directory Permissions
The user_data_dir you specified (/opt/testing/data/temp/) needs proper read/write permissions for the user running your script. If Chrome can't access this directory, it will fail to start.
Check permissions:
ls -ld /opt/testing/data/temp/
Fix them if needed:
chown -R your_username:your_username /opt/testing/data/temp/ chmod -R 755 /opt/testing/data/temp/
Full Corrected Example Code
Here's how your script should look with all these fixes applied:
import undetected_chromedriver as uc CHROME_DRIVER_PATH = '/usr/bin/chromedriver' options = uc.ChromeOptions() options.user_data_dir = '/opt/testing/data/temp/' # Add each Chrome flag separately options.add_argument('--no-first-run') options.add_argument('--no-service-autorun') options.add_argument('--password-store=basic') options.add_argument('--lang=en-GB') options.add_argument('--log-level=0') # Use local driver, disable auto-install driver = uc.Chrome( executable_path=CHROME_DRIVER_PATH, options=options, auto_install=False ) # Test the setup try: driver.get("https://www.google.com") print(f"Success! Page title: {driver.title}") finally: driver.quit()
Start with fixing the argument format first—that's the most likely fix for your immediate connection error. If that doesn't work, move on to the other steps one by one.
内容的提问来源于stack exchange,提问作者Manu

