Reputation: 51
I am trying to use headless chrome
with selenium
, I created a symlink for path to chromedriver into /usr/local/bin, but when I run
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
browser = webdriver.Chrome()
browser.get('http://www.google.com')
I got the error as
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "/opt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/opt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
File "/opt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/opt/anaconda2/envs/tensorflow/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 3.10.0-327.22.2.el7.x86_64 x86_64)
Also it generates a core.*****, a core file dot with five random numbers, which is more than 200MB large.
Versions:
Selenium 3.0.0b3
Google Chrome 60.0.3112.113
chromedriver 2.30
Could anyone help me figure out what is wrong please?
Upvotes: 5
Views: 17654
Reputation: 857
it works for me in debian system. Add following.
chrome_options.add_argument('--no-sandbox')
And I installed apt-get install -y chromium
in debian .
Upvotes: 3
Reputation: 41
I was having the same problem on centos7.1 because I was the root user, and it was resolved after adding the code chrome_options.add_argument('--no-sandbox')
here is my code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://www.google.com/')
print driver.current_url
driver.quit()
Upvotes: 4
Reputation: 5728
I was having a problem getting headless Chrome to work on Ubuntu 14.04, it was dying with the same unknown error: Chrome failed to start: crashed
error, I traced it back to a dependency problem that the Selenium driver was oblivious to.
To diagnose I tried running headless Chrome directly and got:
$ google-chrome --headless "http://example.com"
NSS_VersionCheck("3.26") failed. NSS >= 3.26 is required.
This is what libnss3
had installed:
$ dpkg -s libnss3
Package: libnss3
Version: 2:3.23-0ubuntu0.14.04.1
So I switched over to libnss3-dev
:
$ apt-get install libnss3-dev
$ dpkg -s libnss3
Package: libnss3
Version: 2:3.28.4-0ubuntu0.14.04.3
And retried on the command line:
$ google-chrome --headless "http://example.com"
$ echo $?
0
And sure enough, once headless Chrome was working on the command line it started working in Selenium also.
These are the versions I am currently using:
$ google-chrome --version
Google Chrome 62.0.3202.94
$ chromedriver --version
ChromeDriver 2.33.506092
$ $ pip freeze |grep selenium
selenium==3.7.0
Upvotes: 3
Reputation: 193188
As you are trying to open Chrome browser in headless mode, you don't need to create any extra settings or symlink. Save/Store the chromedriver
anywhere on your system and access the location by its absolute path. To include the argument --headless
you need to take help of Options
class and pass the object while initializing the webdriver
object as below:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()
Console Output:
Page Title is : Google
Upvotes: 0