Bob
Bob

Reputation: 289

How to run chrome headless browser

I have been trying to set up chromes headless browser on my mac but I am getting errors.

I tried following these tutorials for reference:

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

and using these stackoflow pages

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

The headless browser worked on phantomjs but I know selenium no longer wants us to use this. This is what I am running right now: (almost exactly one the responses on 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")

this is my response:

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)

Upvotes: 3

Views: 4821

Answers (2)

Shakesbeer
Shakesbeer

Reputation: 91

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)

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193298

The error does gives us the hint as follows :

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)

The error says that the ChromeDriver didn't find the Chrome binary in the expected location.

There can be two solutions as follows :

  • 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")
    

Alternative

Invoking Google Chrome Browser in Headless Mode programatically have become much easier with the availability of the method set_headless(headless=True) as follows :

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

Upvotes: 4

Related Questions