2

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.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
  • This might be obvious to most but what is this file `""` the traceback refers to? Also, you'll need to set `firefoxCapabilities` and perhaps a `firefoxProfile` for a headless browser. – C. Peck Mar 21 '19 at 16:45
  • @C.Peck "" error should be thrown from Selenium webdriver but not quite sure why it is throwing such error. Can you explain a bit more on how to set firefox Capabilities and Profile or please provide a link to refer to – Bill Clinton J Mar 21 '19 at 16:51
  • I'll start with the caveat that I am completely unfamiliar with xvfb and I've never tried running selenium from the python command line (only by invoking scripts). I'll add an answer with a script that might solve this for you. – C. Peck Mar 21 '19 at 17:00
  • See @MortenB comments within the discussion [How to make firefox headless programmatically in Selenium with python?](https://stackoverflow.com/questions/46753393/how-to-make-firefox-headless-programmatically-in-selenium-with-python/46768243#46768243). MOZ_HEADLESS=1 python manage.py test did the trick! No need for xvfb-run anymore !!! – undetected Selenium Mar 22 '19 at 10:51
  • @BillClintonJ, how is going? Did you try my answer? Thanks. – Ratmir Asanov Mar 22 '19 at 12:32
  • @RatmirAsanov - Yes I tried to update Firefox but I was getting multiple errors. Latest firefox require gtk3 libraries but Centos support only gtk2. Need to find what latest version of firefox does Centos 6.5 support. Any ideas, please advice XPCOMGlueLoad error for file /home/shrao/firefox/firefox/libmozgtk.so: libgtk-3.so.0: cannot open shared object file: No such file or directory Couldn't load XPCOM. – Bill Clinton J Apr 05 '19 at 13:21
  • @RatmirAsanov - I suppose old version of firefox is the cause for the issue. On Centos 6.5 I am not able to install anything more than v52.2.0 of Firefox. I was able to install FF 60.5 on Centos 6.9 where everything worked fine, also on Centos 7.4 I was able to install more recent version of FF there everything worked fine. So older version of firefox should be the cause, I was not able to install more recent version of FF on Centos 6.5 due to GTK3 library support problem – Bill Clinton J Apr 05 '19 at 16:33

1 Answers1

0

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()
C. Peck
  • 3,641
  • 3
  • 19
  • 36