如何运行Chrome无头浏览器

3

我一直在尝试在我的Mac上设置Chrome的无头浏览器,但是我一直遇到错误。

我尝试参考以下教程:

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

并使用以下Stack Overflow页面:

如何让Chromedriver在无头模式下运行?selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH error with chrome

无头浏览器可以在PhantomJS上工作,但我知道Selenium不再希望我们使用它。 这是我现在正在运行的代码(几乎与Stack Overflow上的回复一致):

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)
2个回答

4
错误提示提供了以下提示:

错误提示提供了以下提示:

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 二进制文件。

可以有两种解决方案:

  • As per the Requirements the Chrome installation needs to be in a definite location.

  • As an alternative you can pass the absolute path of the Chrome binary through an instance of Options class as follows :

    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    
  • Finally while initiating the WebDriver and WebClient you need to send the argument Key executable_path along with the Value 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")
    

替代方案

以编程方式在无头模式下调用Google Chrome浏览器现在变得更加容易,因为现在有了以下方法:set_headless(headless=True)

  • Documentation :

    set_headless(headless=True)
        Sets the headless argument
    
        Args:
            headless: boolean value indicating to set the headless option
    
  • Sample Code :

    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()
    

你知道我发布的答案链接为什么能正常工作吗? - Bob

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

option = Options()
option.headless = True
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', options = option)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接