Reputation: 21
I am trying to run a Python + Selenium script in headless mode with Firefox using Xvfb but I am getting errors. There is not much documents or guides available for Xvfb to troubleshoot the issue so looking for assistance here.
Environment info:
OS: CentOS release 6.5 (Minimal installation)
Xvfb: xorg-x11-server-Xvfb-1.15.0
Firefox: 52.8.0
geckodriver: 0.24.0
Python: 3.6.7
Steps followed:
Once done installing the above-mentioned requirements. I started a virtual display with:
$Xvfb :1 -ac &
Also, I tried with:
$Xvfb :1 -screen 0 1024x768x24 -extension RANDR &
And then I set Display variable:
export DISPLAY=:1
When I tried to initiate Selenium WebDriver in Python console I am getting the error Connection refused:
> from selenium import webdriver
> from pyvirtualdisplay import Display
> display = Display(visible=0, size=(800, 600))
> display.start()
> driver = webdriver.Firefox()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
Any help or suggestion will be much appreciated.
Upvotes: 2
Views: 1322
Reputation: 3711
There is a wrapper around xvfb called PyVirtualDisplay that seems designed for this exact solution. If you simply do a pip install pyvirtualdisplay
the following script should run a headless Firefox window:
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
# now Firefox will run in a virtual display.
# you will not see the browser.
browser = webdriver.Firefox(executable_path="/Users/username/Location/geckodriver")
browser.get('http://www.google.com')
print browser.title
browser.quit()
display.stop()
Upvotes: 0