如何运行chrome无头浏览器

时间:2018-03-30 21:57:32

标签: python-3.x selenium selenium-chromedriver headless-browser google-chrome-headless

我一直在尝试在我的Mac上设置chromes无头浏览器但我收到错误。

我尝试按照这些教程进行参考:

https://intoli.com/blog/running-selenium-with-headless-chrome/ https://duo.com/decipher/driving-headless-chrome-with-python

并使用这些stackoflow页面

How to get Chromedriver in headless mode to run headless?selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH error with chrome

无头浏览器工作在phantomjs但我知道selenium不再希望我们使用它。这就是我现在正在运行的:(几乎只有一个堆栈溢出的响应)

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

chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://www.duo.com")
print("Chrome Browser Initialized in Headless Mode")

这是我的回答:

Traceback (most recent call last):
  File "headless_browser.py", line 47, in <module>
    driver = webdriver.Chrome(chrome_options=chrome_options)
  File "/Users/BCohen/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
  File "/Users/BCohen/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/Users/BCohen/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/Users/BCohen/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/Users/BCohen/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.36.540469 (1881fd7f8641508feb5166b7cae561d87723cfa8),platform=Mac OS X 10.13.2 x86_64)

1 个答案:

答案 0 :(得分:3)

错误确实给我们提示如下:

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.36.540469 (1881fd7f8641508feb5166b7cae561d87723cfa8),platform=Mac OS X 10.13.2 x86_64)

错误表明 ChromeDriver 在预期位置找不到 Chrome 二进制文件。

可以有以下两种解决方案:

  • 根据Requirements Chrome 安装需要位于明确的位置。

  • 作为替代方案,您可以通过Options类的实例传递 Chrome 二进制文件的绝对路径,如下所示:

    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    
  • 最后,在启动 WebDriver WebClient 时,您需要发送参数 Key executable_path 使用 chrome_path

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    options = Options()
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    chrome_path = "/home/ec2-user/chrome/chromedriver"
    driver = webdriver.Chrome(executable_path=chrome_path, chrome_options=options)
    driver.get("http://www.duo.com")
    print("Chrome Browser Initialized in Headless Mode")
    

替代

使用方法set_headless(headless=True)的可用性,以编程方式在 无头模式 中以编程方式调用 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(chrome_options=chrome_options, executable_path=r'C:\path\to\chromedriver.exe')
    driver.get("http://google.com/")
    print ("Headless Chrome Initialized")
    driver.quit()