57

I am getting this error when I run my tests with Selenium using chromedriver.

selenium.common.exceptions.WebDriverException: Message:
unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.9.248316,platform=Linux 3.8.0-29-generic x86)

I did download google-chrome stable and also chromedriver and have used this code to start the browser.

driver = webdriver.Chrome('/usr/local/bin/chromedriver')

Any suggestions anyone?

ouflak
  • 2,458
  • 10
  • 44
  • 49
Saheb
  • 1,666
  • 3
  • 18
  • 24

11 Answers11

44

For Linux :

Start the Display before start the Chrome. for more info click here

from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 800))  
display.start()
driver = webdriver.Chrome()
Community
  • 1
  • 1
Vicky
  • 5,098
  • 2
  • 33
  • 31
  • 9
    hi, can you explain why is pyvirtualdisplay needed? – Anirudh Dec 24 '16 at 07:24
  • 5
    One need to run the commands >> "pip install pyvirtualdisplay" and >> "sudo apt-get install xvfb" – vikramvi Dec 08 '17 at 17:05
  • What does the visible flag do? I cannot see any display with or without specifying it – Ryan w Jul 07 '21 at 19:55
  • A great solution! I ran my driver with the option --headless, which suddenly stopped being an option and caused errors and distress. This solved the issue right up. @vikramvi note about installing xvfb was also helpfull. Thank you! – Konemiees Sep 12 '21 at 19:06
34

To help debug this problem you can use the service_log_path and service_args arguments to the selenium webdriver to see output from the chromedriver:

service_log_path = "{}/chromedriver.log".format(outputdir)
service_args = ['--verbose']
driver = webdriver.Chrome('/path/to/chromedriver',
        service_args=service_args,
        service_log_path=service_log_path)

I was getting this same exception message and found two ways to get past it; I'm not sure if the OP's problem is the same, but if not, the chromedriver log will hopefully help. Looking at my log, I discovered that the chromedriver (I tried 2.9 down to 2.6 while trying to fix this problem) decides which browser to run in a very unexpected way. In the directory where my chromedriver is located I have these files:

$ ls -l /path/to/
-rwx------  1 pjh grad_cs 5503600 Feb  3 00:07 chromedriver-2.9
drwxr-xr-x  3 pjh grad_cs    4096 Mar 28 15:51 chromium

When I invoke the chromedriver using the same python code as the OP:

driver = webdriver.Chrome('/path/to/chromedriver-2.9')

This leads to the exception message. In the chromedriver.log I found this message:

[1.043][INFO]: Launching chrome: /path/to/chromium ...

Unbelievable! The chromedriver is trying to use /path/to/chromium (which is not an executable file, but a directory containing source code) as the browser to execute! Apparently chromedriver tries to search the current directory for a browser to run before searching my PATH. So, one easy solution to this problem is to check the directory where the chromedriver is located for files/directories like chrome and chromium and move them to a different directory than the chromedriver.

A better solution is to explicitly tell selenium / chromedriver which browser to execute by using the chrome_options argument:

options = webdriver.ChromeOptions()
options.binary_location = '/opt/google/chrome/google-chrome'
service_log_path = "{}/chromedriver.log".format(outputdir)
service_args = ['--verbose']
driver = webdriver.Chrome('/path/to/chromedriver',
        chrome_options=options,
        service_args=service_args,
        service_log_path=service_log_path)

The chromedriver.log now shows:

[0.999][INFO]: Launching chrome: /opt/google/chrome/google-chrome ...

as expected.

pjhsea
  • 841
  • 9
  • 13
  • 4
    Thanks man. But I already did figure out a way. It happens that by just changing from `driver = webdriver.Chrome('/usr/local/bin/chromedriver')` to `driver = webdriver.Chrome()`, I was able to make my code run. I don't know why that happened, but it just did! @pjhuw – Saheb Mar 31 '14 at 05:48
  • 1
    thanks for showing how to output to the chromedriver.log. really useful – frequent Jun 28 '18 at 08:13
  • Thanks for providing way to find the logs of chrome driver. It helped me to find the issue. – Simbu Oct 11 '18 at 07:12
  • 2
    Another reason - I was running as root without --no-sandbox switch and chrome did not like it. Switched to another user and voila. – Gunjit Sep 21 '19 at 04:09
  • OMG. This worked for me. You're a saviour. – Aakash Dusane Feb 16 '23 at 08:21
28

An alternative solution of using a virtual display is the headless mode.

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1420,1080')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
Siyu
  • 11,187
  • 4
  • 43
  • 55
13

If using Linux make sure you are not running as root. That what gave me the error.

GuySoft
  • 1,723
  • 22
  • 30
  • 3
    If you're using a CI tool, you might be running as root without realizing it (or having control over it). I unfortunately had to use `--no-sandbox` to get it working. – user2561747 Jan 13 '18 at 00:20
  • 2
    Wowwwwww, it worked for me, but anybody knows the reason? – Umair Ayub Jul 23 '18 at 16:20
  • GuySoft, hi, I try all that i see in stackoverflow, but do not solved error. How can I change run other user? without root. – Tornike Kharitonishvili Aug 18 '23 at 06:13
  • @TornikeKharitonishvili Your python script should run as another user, for example you could dertamine the user for ID 1000, which in a single-user system tends to be the default one``sudo -u $(awk -F':' -v uid=1000 '$3 == uid { print $1 }' /etc/passwd) python3 ./script.py `` – GuySoft Aug 25 '23 at 07:17
7

Someone already mentioned about --no-sandbox option, but to expand on it: make sure, it's the first option you pass:

        System.setProperty("webdriver.chrome.driver",
                Paths.get("setups", driverFolder, driverFile).toAbsolutePath().toString());

        ChromeOptions options = new ChromeOptions();
        Map<String, Object> prefs = new HashMap<>();
        prefs.put("intl.accept_languages", "English");
        options.setExperimentalOption("prefs", prefs);

        options.addArguments("--no-sandbox");
        options.addArguments("--disable-features=VizDisplayCompositor");
        options.addArguments("--incognito");
        options.addArguments("enable-automation");
        options.addArguments("--headless");
        options.addArguments("--window-size=1920,1080");
        options.addArguments("--disable-gpu");
        options.addArguments("--disable-extensions");
        options.addArguments("--dns-prefetch-disable");
        options.setPageLoadStrategy(PageLoadStrategy.NORMAL);

        options.addArguments("enable-features=NetworkServiceInProcess");

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("marionette", true);
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);

        WebDriver driver = new ChromeDriver(capabilities);
        driver.manage().timeouts().implicitlyWait(15, SECONDS);
        driver.manage().timeouts().pageLoadTimeout(15, SECONDS);

When it was added after other options, I got the error.

parsecer
  • 4,758
  • 13
  • 71
  • 140
2

You may be able to fix this issue by making sure your version of chromedriver is right for the version of Chrome you have installed, which you can check here. You will also need to remove your current version of chromedriver before installing the new one, as described in Delete Chromedriver from Ubuntu

mayaPapaya
  • 143
  • 7
1

This issue resolved using below steps

  1. Install Xvfb Centos 7 : yum install chromedriver chromium xorg-x11-server-Xvfb

  2. update chrome driver Centos 7 : wget https://chromedriver.storage.googleapis.com/2.40/chromedriver_linux64.zip

G.S
  • 10,413
  • 7
  • 36
  • 52
praveen kedar
  • 188
  • 2
  • 5
0

I was faced with the same issue and fixed it by installing Chrome in:

C:\Users\..\AppData\Local\Google\Chrome\Application

You can do this by running the Chrome Setup and saying no when prompted by the User Account Control.

quasarseeker
  • 167
  • 9
0

I got the same error when I crawl something using scrapy + selenium + chrome driver on Centos 7,and the method of following url solved my problem.

yum install mesa-libOSMesa-devel gnu-free-sans-fonts

refer:https://bugs.chromium.org/p/chromium/issues/detail?id=695212

Varad Mondkar
  • 1,441
  • 1
  • 18
  • 29
0

Another solution for selenium webdriver is X virtual frame buffer:

with Xvfb() as _:
    timeout_request = ConfigTargetsManager.target_global_configs.get('timeout_request', 10)
    driver = webdriver.Chrome(executable_path=ConfigTargetsManager.target_global_configs.get('chrome_browser_path',
                                                                                             '/usr/lib/chromium-browser/chromedriver'))
    driver.get(url)
Saleh
  • 1,819
  • 1
  • 17
  • 44
0

Ubuntu 22.04. May be useful for someone. I got this bug when trying to get selenium to work with a version of Brave (Deb) installed from the brave.com repository.

Additionally installed Brave from the snap image, added it:

options.add_argument('--remote-debugging-port=9224')
options.binary_location = '/snap/bin/brave'

This solved the problem.

Tim
  • 1
  • 1