Search code examples
linuxgoogle-chromeselenium-webdriverselenium-chromedriverubuntu-server

Unable to run Selenium 4.13.0 and Python 3 on Linux server


I run the Selenium script in Python without any issue.

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


options = Options()
options.page_load_strategy = 'eager'
options.add_argument('--headless=new')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=options)

My chrome version is 116.0.5845.179. As of selenium 4.10.0 the driver manager is fully integrated, and will silently download drivers as needed. There is not a need to manually install Chrome driver anymore. So, I don't have chrome driver installed on my laptop.

However, when I do the same on my Linux server, the browser fails to start. The Linux server is Ubuntu and has no graphical user interface. I installed chrome of version 117.0.5938.149 on the server. I received the following error when I ran the script :

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally.
  (session not created: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

When I try to run "google-chrome" on the terminal on the server, I receive the following error:

[34407:34407:1008/225549.976145:ERROR:zygote_host_impl_linux.cc(100)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

The error messages don't tell me much besides I can't run chrome. How do I run the script in headless mode of the browser on the server?


Solution

  • I solved the problem by adding the following line to the options

    options.add_argument('--no-sandbox')