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

Ubuntu Xenial 16.04服务器上Selenium+ChromeDriver脚本无限挂起求助

Troubleshooting Your Hanging Selenium Script on Ubuntu 16.04

Hey there, let’s figure out why your Python Selenium script is hanging indefinitely on your Ubuntu 16.04 server. I’ve dealt with this exact scenario a bunch of times, so here are the most common fixes to try, ordered by how likely they are to resolve your issue:

1. You’re Missing Headless Mode (Critical for Server Environments)

Ubuntu servers run without a graphical user interface (GUI), but Chrome defaults to needing one to launch. If you don’t enable headless mode, the browser will hang waiting for a GUI that doesn’t exist.

Fix this by adding these arguments to your Chrome options in script.py:

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

chrome_options = Options()
# Enable headless mode (no GUI required)
chrome_options.add_argument('--headless')
# Required for Ubuntu servers to bypass sandbox restrictions
chrome_options.add_argument('--no-sandbox')
# Fixes "DevToolsActivePort file doesn't exist" errors
chrome_options.add_argument('--disable-dev-shm-usage')
# Optional: Set a window size (some pages behave unpredictably without it)
chrome_options.add_argument('--window-size=1920,1080')

driver = webdriver.Chrome(options=chrome_options)

2. Chrome and ChromeDriver Version Mismatch

Selenium requires exact version compatibility between Chrome and ChromeDriver. Even a minor version difference can cause hangs, crashes, or unresponsive sessions.

  • First, check your installed Chrome version:
    google-chrome --version
    
  • Download the matching ChromeDriver version (make sure it’s compatible with Ubuntu 16.04’s architecture).
  • Replace your current ChromeDriver with the new one, and ensure it has execute permissions:
    chmod +x /path/to/your/chromedriver
    

Pro tip: To avoid manual version checks, use webdriver-manager to auto-handle driver updates (note: Ubuntu 16.04’s default Python 3.5 works with older webdriver-manager releases):

pip install webdriver-manager==3.8.6

Then update your script to use it:

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

chrome_options = Options()
# Add the headless/sandbox arguments from step 1 here

driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)

3. Permissions or Sandbox Restrictions

Ubuntu’s security settings might block Chrome from running properly in a server context. The --no-sandbox flag helps with this, but double-check:

  • Ensure the user running the script has permissions to execute Chrome and ChromeDriver.
  • Avoid running the script as root (Selenium often misbehaves with root privileges; use a regular sudo-enabled user instead).

4. Unbounded Page Load Waits

Your script might be hanging because it’s waiting indefinitely for a page to load or an element to appear. Add explicit timeouts to prevent this:

# Set a 30-second timeout for page loads
driver.set_page_load_timeout(30)

# Set a 10-second implicit wait for elements to appear
driver.implicitly_wait(10)

For more reliability, use explicit waits for specific elements:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Wait up to 15 seconds for an element to become visible
element = WebDriverWait(driver, 15).until(
    EC.visibility_of_element_located((By.ID, "target-element-id"))
)

5. Analyze the Interruption Error Log

When you manually interrupt the script, the traceback will point to the exact line causing the hang. Common errors to look for:

  • session not created: This version of ChromeDriver only supports Chrome version X: Clear version mismatch (fix with step 2).
  • unknown error: DevToolsActivePort file doesn't exist: Fixed by adding --disable-dev-shm-usage and --no-sandbox (step 1).
  • TimeoutException: Means a page/element took too long to load (fix with step 4).

If after trying these steps the issue persists, share the full traceback from the interruption—that’ll help narrow it down further!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:22:15