Can't run Chromedriver in headless mode using Selenium

Question:

running my selenium script I ran into an error that I don’t know how to pass. I’ve googled and tried various solutions but they didn’t work.
Here is my code:

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

chrome_driver_binary = '/home/user/Projects/myproject/chromedriver'
options = Options()
options.add_argument("--start-maximized")
options.add_argument("--no-sandbox") 
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(executable_path=chrome_driver_binary, options = options)
driver.get('http://www.ubuntu.com/')

And everytime I run this I get the error "The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed."

However, if I comment out the line "options.add_argument("–headless")" my code works perfectly fine. But I can’t leave that commented out because I need that script to run in headless mode.

I am using Ubuntu 22.04 on VirtualBox 7, Python 3.10.6 and run my code in virtual environment.

Asked By: syrok

||

Answers:

You can use the newer headless mode (for Chrome 109 and newer):
https://stackoverflow.com/a/73840130/7058266

options.add_argument("--headless=new")

If you have an older version of Chrome, the syntax will be different. (See the link for details.)

Answered By: Michael Mintz