如何为指定路径的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=newargument 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=newwith just--headless(the legacy headless mode). - The
--disable-gpuflag 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
相关产品推荐
相关产品推荐

