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

如何为指定路径的Chromedriver启用Headless无界面模式?

Switching ChromeDriver to Headless Mode

Got it! To run Chrome in headless mode (without the visible GUI window), you just need to configure ChromeOptions and pass them to your WebDriver instance. Here's how to adjust your existing code:

Modified Code

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

# Set up Chrome options for headless execution
chrome_options = Options()
# Use the modern headless mode (recommended for Chrome 112+)
chrome_options.add_argument("--headless=new")
# Optional: Disable GPU (helps with compatibility on some systems)
chrome_options.add_argument("--disable-gpu")

# Initialize the driver with the specified path and headless options
driver = webdriver.Chrome(
    executable_path=r"C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\bin\chromedriver.exe",
    options=chrome_options
)
driver.get("https://www.example.com/")

# Remember to close the driver when your scraping is done
driver.quit()

Key Notes:

  • The --headless=new argument enables the modern headless mode, which behaves almost identically to a regular Chrome window (supports JavaScript, extensions, etc.). This is the recommended option for Chrome versions 112 and above.
  • If you're using an older Chrome version (pre-112), replace --headless=new with just --headless (the legacy headless mode).
  • The --disable-gpu flag is optional but often added to avoid GPU-related errors, especially on Linux or older systems.

That's it! Your ChromeDriver will now run in the background without a visible window.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:35:28