1

I'm trying to install and use Selenium chromedriver on my virtual ubuntu shell, I follow step by step various tutorial but it seems there is still something going wrong ... And after many research on the issue I can't find any answer.

Here is the little code I'm trying to run :

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

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-extensions')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://google.com')

I added the chrome_options thanks to the advice I found on various topic. However I don't really understand why is it necessary?

Unfortunately, my program send me the following error and here I am, stuck and I don't know what to do:

/home/lclis/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:7: DeprecationWarning: use options instead of chrome_options
  import sys
---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-4-259faa721232> in <module>
      5 chrome_options.add_argument('--no-sandbox')
      6 chrome_options.add_argument('--disable-extensions')
----> 7 driver = webdriver.Chrome(chrome_options=chrome_options)
      8 driver.get('https://google.com')

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py 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()

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py 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)

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py 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']

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py 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))

~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py 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/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

Anyone already had this issue or have any suggestion on it?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Louis Clisson
  • 51
  • 2
  • 6
  • Does this answer your question? [Why chrome browser doesn't launch when running Selenium chrome driver on Ubuntu 18.04](https://stackoverflow.com/questions/59989038/why-chrome-browser-doesnt-launch-when-running-selenium-chrome-driver-on-ubuntu) – SiKing Jan 30 '20 at 18:13
  • Did you find a solution for this issue? I'm having the same issue in a Ubuntu 20 VPS and so far, the only thing I've got is that as Ubuntu doesn't have installed a GUI, it can't make use of chromedriver other than passing the `--headless` argument – puppet Sep 01 '20 at 21:55

3 Answers3

1

Thumb rule

A common cause for Chrome to crash during startup is running Chrome as root user (administrator) on Linux. While it is possible to work around this issue by passing --no-sandbox flag when creating your WebDriver session, such a configuration is unsupported and highly discouraged. You need to configure your environment to run Chrome as a regular user instead.


This first error message...

/home/lclis/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:7: DeprecationWarning: use options instead of chrome_options
  import sys
---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-4-259faa721232> in <module>
      5 chrome_options.add_argument('--no-sandbox')
      6 chrome_options.add_argument('--disable-extensions')
----> 7 driver = webdriver.Chrome(chrome_options=chrome_options)

...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.


As per the Release Notes of Selenium's client v3.8.0 chrome_options is deprecated:

Browser option parameters are now standardized across drivers as options. firefox_options, chrome_options, and ie_options are now deprecated.


Solution

You need to use options instead of chrome_options while initializing ChromeDriver/Chrome session. So effectively your code block will be:

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

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-extensions')
driver = webdriver.Chrome(executable_path='/path/to/chromedriver', options=chrome_options)
driver.get('https://google.com')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi thanks for your feeback ! I changed the parameters as you suggest me but the same error is happening. My OS is windows but I work on jupyter notebook with Ubuntu from the Windows Store, I downloaded the linux chromedriver. Do you think it can explain this error? – Louis Clisson Jan 30 '20 at 21:25
  • @LouisClisson If the underlying os is **Ubuntu**, I guess you are on the right track. Can you checkout the updated answer and let me know the status. – undetected Selenium Jan 30 '20 at 21:30
  • "You need to configure your environment" - any links or hints to how to set it up properly? – dasWesen Jan 23 '22 at 21:01
0

You can run a selenium crawler on a server (ubuntu with no GUI, no X window available in the box) with this code snippet :

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

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')

driver = webdriver.Chrome(executable_path='/usr/lib/chromium-browser/chromedriver', options=chrome_options)
driver.get('https://google.com')
mounirboulwafa
  • 1,587
  • 17
  • 18
0

I was googling for ruby error, but found this. For some it may be helpful.

I wrongly set capybara session (which relies on selenium on its own)

# WRONG
session = Capybara::Session.new(:selenium_chrome)

# CORRECT, see headless added
session = Capybara::Session.new(:selenium_chrome_headless)
zhisme
  • 2,368
  • 2
  • 19
  • 28