'Selenium Chromedriver hangs at browser initialisation (browser = webdriver.Chrome()) w. Python on Raspberrry Pi3 / or crashes :c

  • Hello!
  • I am not able to format below code as code lol:

from selenium import webdriver

browser = webdriver.Chrome()

When doing this; I am getting:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/.local/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "/home/pi/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/pi/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/pi/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/pi/.local/lib/python3.9/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: Chrome failed to start: crashed.
  (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.)

Therefore, I followed suggestions on several Sites, for example This one, to include some options as follows:

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

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
browser = webdriver.Chrome(options=chrome_options)

very unpleasing, although, is that instead of getting an error or seeing the program successfully perform the webdriver's initialization, the program just stops giving me any output, and doesn't seem to timeout either (I waited like 10 mins while writing this question).

I am running Raspi OS 64-bit (Bullseye), and the system is freshly installed. Tested on both the most recent Selenium and on Selenium Version 3.141.0. The Version of Chromedriver is 98.0.4758.106, the same as Chromium-Browser.

How cant I get Selenium up and running? Best regards and looking forward to your suggestions! Reisbrot



Solution 1:[1]

If I follow as shown here: WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser

I am able to get the driver up and running, yet it fails to ever acknowledge that a page is loaded. Therefore we have to introduce Desired Capabilities to state not to wait for any page to be loaded, and set a timeout manually: Don't wait for a page to load using Selenium in Python

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("start-maximized"); #// open Browser in maximized mode
options.add_argument("disable-infobars"); #// disabling infobars
options.add_argument("--disable-extensions"); #// disabling extensions
options.add_argument("--disable-gpu"); #// applicable to windows os only
options.add_argument("--disable-dev-shm-usage"); #// overcome limited resource problems
options.add_argument("--no-sandbox"); #// Bypass OS security model
caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "none"   # Do not wait for full page 
browser = webdriver.Chrome(options=options, desired_capabilities=caps)
browser.get(URL)  
time.sleep(9) #This can be done better 

Solution 2:[2]

Arguments order need a change.

chrome_options = Options()
chrome_options.add_argument('--no-sandbox') 
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-dev-shm-usage')
browser = webdriver.Chrome(options=chrome_options)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Reisbrot
Solution 2 Ravi Parigadupu