Tech Girl
Tech Girl

Reputation: 199

Chrome crashes when running selenium python3 script as sudo

I have a python script where I login to a website using Selenium. Following is the snippet

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
browser = webdriver.Chrome("/path/to/chromdriver", desired_capabilities=chrome_options.to_capabilities())
WebDriverWait(browser, 10)
browser.maximize_window()
browser.get(url)
browser.find_element_by_xpath('//*[@id="mat-input-0"]').send_keys(username)
browser.find_element_by_xpath('//*[@id="mat-input-1"]').send_keys(pass)
browser.find_element_by_xpath('/html/body/app-root/app-login/mat-card/mat-card-content/form/div/button/span').click()

The script runs perfectly fine when run as python3 test.py. But when I run it as sudo python test.py it crashes with following error:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
  (chrome not reachable)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

What changes would I need to do to be able to run as sudo?

Upvotes: 4

Views: 1552

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193268

From the ChromeDriver - WebDriver for Chrome:

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. Please configure your environment to run Chrome as a regular user instead.


Conclusion

You need to execute your tests as a regular user


References

You can find a couple of relevant discussions in:

Upvotes: 1

Related Questions