3

I am trying to get selenium running with a headless chrome in the following environment:

  • OS: Ubuntu 16.04.6
  • Chrome: 77.0.3865.90 (Official Build) (64-bit)
  • ChromeDriver: 77.0.3865.40
  • Selenium server version: 3.141.59, revision: e82be7d358

I've been reading about it at developer.google.com and followed the recommended Running Selenium with Headless Chrome post. I followed the steps in the ipython REPL:

from selenium import webdriver

options = webdriver.ChromeOptions()

options.binary_location = '/usr/local/bin'

options.add_argument('headless')

options.add_argument('window-size=1200x600')

driver = webdriver.Chrome(chrome_options=options)

But this results in the following error:

WebDriverException                        Traceback (most recent call last)
<ipython-input-6-658404e774ae> in <module>()
----> 1 driver = webdriver.Chrome(chrome_options=options)

/home/user/.local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.pyc in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
     79                     remote_server_addr=self.service.service_url,
     80                     keep_alive=keep_alive),
---> 81                 desired_capabilities=desired_capabilities)
     82         except Exception:
     83             self.quit()

/home/user/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
    155             warnings.warn("Please use FirefoxOptions to set browser profile",
    156                           DeprecationWarning, stacklevel=2)
--> 157         self.start_session(capabilities, browser_profile)
    158         self._switch_to = SwitchTo(self)
    159         self._mobile = Mobile(self)

/home/user/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in start_session(self, capabilities, browser_profile)
    250         parameters = {"capabilities": w3c_caps,
    251                       "desiredCapabilities": capabilities}
--> 252         response = self.execute(Command.NEW_SESSION, parameters)
    253         if 'sessionId' not in response:
    254             response = response['value']

/home/user/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

/home/user/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.pyc in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/local/bin is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

I've been looking around and found this answer. I've seen there the 'headless' argument is passed like this: add_argument("--headless"). I've tried to add it with the '--' but with the same exception as result.
In this comment the --no-sandbox argument is recommended as well but adding this argument doesn't change anything in my case neither.

How can I debug and fix this error?

Update based on the answer from Wonka:

I've changed options.add_argument('headless') to options.add_argument('--headless') and options.add_argument('window-size=1200x600') to options.add_argument('--window-size=1200x600') with the same error as above.

Then as recommended I tried to set resolution after driver = webdriver.Chrome(chrome_options=options) but this line already fails.

frianH
  • 7,295
  • 6
  • 20
  • 45
Daniel
  • 971
  • 9
  • 23
  • You might want to switch to FireFox driver. I had also problems with headless Chrome.which disappeared once I switched to FireFox. – Zvi Jan 14 '21 at 21:14

1 Answers1

1

Multiple changes, full path

#Pass binary on init object
##options.binary_location = '/usr/local/bin/chromedriver'

options.add_argument('--headless')

Extra options on my code

options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")

Edit2:

driver = webdriver.Chrome(chrome_options=options, executable_path="/usr/local/bin/chromedriver")

To set resolution I use this, after init driver object

driver.set_window_size(1280, 1024)
Wonka
  • 1,548
  • 1
  • 13
  • 20
  • Seems to work. I am not getting any error on init anymore. Passing the executable path directly did it. – Daniel Oct 08 '19 at 11:12