如何通过Selenium配置ChromeDriver以无头模式启动Chrome浏览器?

2024-04-26 00:26:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个python脚本来进行web scrape,并已经开始使用Chromedriver作为其中一个包。我希望这个在后台操作,不需要任何弹出窗口。我在chromedriver上使用了“headless”选项,它似乎完成了不显示浏览器窗口的工作,但是,我仍然看到.exe文件正在运行。看我说的截图。Screenshot

这是我用来启动ChromeDriver的代码:

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
options.add_argument('headless')
options.add_argument('window-size=0x0')
chrome_driver_path = "C:\Python27\Scripts\chromedriver.exe"

我试图做的是将选项中的窗口大小更改为0x0,但由于.exe文件仍然弹出,我不确定这是否起作用。

你知道我该怎么做吗?

我正在使用Python 2.7fyi


Tags: 文件脚本addweb选项浏览器argumentexe
3条回答

当使用Selenium客户端3.11.xChromeDriver v2.38Google Chrome v65.0.3325.181无头模式时,您必须考虑以下几点:

  • 您需要添加参数--headless才能在无头模式下调用Chrome。
  • 对于Windows操作系统您需要添加参数--disable-gpu
  • 根据Headless: make --disable-gpu flag unnecessary--disable-gpu,在Linux系统MacOS上不需要标志。
  • 根据SwiftShader fails an assert on Windows in headless mode--disable-gpu标记在Windows系统上也将变得不必要。
  • 参数start-maximized对于最大化的视区是必需的。
    • 这是有关Viewport的详细信息的链接。
  • 您可能需要添加参数--no-sandbox以绕过操作系统安全模型。

  • 示例Windows代码块:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("--headless") # Runs Chrome in headless mode.
    options.add_argument('--no-sandbox') # Bypass OS security model
    options.add_argument('--disable-gpu')  # applicable to windows os only
    options.add_argument('start-maximized') # 
    options.add_argument('disable-infobars')
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
    driver.get("http://google.com/")
    print ("Headless Chrome Initialized on Windows OS")
    
  • Linux代码块示例:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("--headless") # Runs Chrome in headless mode.
    options.add_argument('--no-sandbox') # # Bypass OS security model
    options.add_argument('start-maximized')
    options.add_argument('disable-infobars')
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(chrome_options=options, executable_path='/path/to/chromedriver')
    driver.get("http://google.com/")
    print ("Headless Chrome Initialized on Linux OS")
    

更新(2018年4月23日)

由于方法^{}的可用性,以编程方式在无头模式下调用Google Chrome浏览器变得容易得多:

  • 文件:

    set_headless(headless=True)
        Sets the headless argument
    
        Args:
            headless: boolean value indicating to set the headless option
    
  • 示例代码:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.set_headless(headless=True)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')
    driver.get("http://google.com/")
    print ("Headless Chrome Initialized")
    driver.quit()
    

Note : --disable-gpu argument is implemented internally.


更新(2018年10月13日)

要在headless模式下调用Chrome浏览器,只需通过Options()类设置--headless属性,如下所示:

  • 示例代码:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.headless = True
    driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')
    driver.get("http://google.com/")
    print ("Headless Chrome Initialized")
    driver.quit()
    

奥特罗

How to make firefox headless programmatically in Selenium with python?


tl;博士

这是Sandbox故事的链接。

应该是这样的:

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

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')  # Last I checked this was necessary.
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)

这对我使用Python3.6很有效,我相信它也适用于2.7。

更新2018-10-26:现在您可以这样做:

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

options = Options()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
  1. .exe仍将运行。根据Google的说法-“以无头模式运行,即不依赖UI或显示服务器。”

  2. 最好在命令行参数前面加上2个破折号,即options.add_argument('--headless')

  3. 在无头模式下,还建议禁用GPU,即options.add_argument('--disable-gpu')

相关问题 更多 >