3
votes

This is not a repost of

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed with ChromeDriver and Selenium in Python

I am using Linux and creating a new profile is not an option. I want to load an existing profile (not create a new one) just like selenium gui can.

I am able to get chromium to function, but not google chrome. Chrome will open but will kick back an

selenium.common.exceptions.WebDriverException: Message: Service /opt/google/chrome/chrome unexpectedly exited. Status code was: 0

error.

I am trying to start google chrome with user directory access, so I can capture existing sessions.

code that fails:

option.add_argument("user-data-dir=/home/user/.config/google-chrome/Default/") #)PATH is path to your chrome profile
driver = webdriver.Chrome('/opt/google/chrome/chrome', options=option)

code that works but launches chromium and not google-chrome:

option.add_argument("user-data-dir=/home/user/snap/chromium/common/.cache/chromium/Default/") #)PATH is path to your>
driver = webdriver.Chrome('/snap/bin/chromium.chromedriver', options=option)

I'm sure I am using the correct executable

htop

I'm pretty sure I have the correct chromedriver driver install

root@Inspiron-laptop:/home/user# pip3 install chromedriver-autoinstaller
Requirement already satisfied: chromedriver-autoinstaller in /usr/local/lib/python3.8/dist-packages (0.2.2)

Just using it incorrectly.

How do I launch google-chrome from within selenium while accessing cache directories?

I am on Ubuntu 20.04

UPDATE:

Full script:

#!/usr/bin/python3

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from seleniumbase import BaseCase
from selenium.webdriver.chrome.options import Options
import time
import random

minptime = 25
maxptime = 120

class MyweClass(BaseCase):
        def method_a():
                option = webdriver.ChromeOptions()
                option.add_argument('--disable-notifications')
                option.add_argument("--mute-audio")
                option.add_argument("user-data-dir=/home/user/.config/google-chrome/Default/") #)PATH is path to your chrome profile
                driver = webdriver.Chrome('/opt/google/chrome/chrome', options=option)
                driver.get("https://world.com/myworld")
                print(f'driver.command_executor._url: {driver.command_executor._url}')
                print(f'driver.session_id: {driver.session_id}')
                time.sleep(18)
                return driver
driver = MyweClass.method_a()

UPDATE II:

Same error using

option.add_argument("user-data-dir=~/.config/google-chrome/Default/")

and

driver = webdriver.Chrome('/opt/google/chrome/google-chrome', options=option)

and

chmod -R 777 /home/user/.config

To ensure user was hitting cache directory as user.

Google chrome info:

enter image description here

1

1 Answers

-1
votes

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 error message...

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.26.436382 (70eb799287ce4c2208441fc057053a5b07ceabac),platform=Linux 4.15.0-109-generic x86_64)

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

As per your code trials seems you are trying to access a Chrome Profile so you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("user-data-dir=C:\\path\\to\\your\\profile\\Google\\Chrome\\User Data\\Profile 2")
    driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
    driver.get("https://www.google.co.in")
    

References

You can find a couple of detailed discussions in: