Joseph D.
Joseph D.

Reputation: 12174

Xvfb: connection refused when running test script

I have a python script which will only be triggered by calling my.ip.address/test. It works fine if I run the PHP code via command line.

However,if I access the test automation via browser using the url specified, it gives me this error:

Traceback (most recent call last): File "scripts/crawler.py",
line 10, in driver = webdriver.Firefox(capabilities={"marionette":True}) File "/usr/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py",
line 152, in __init__ keep_alive=True) File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 98, in __init__ self.start_session(desired_capabilities, browser_profile) File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 188, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 252, in execute self.error_handler.check_response(response) File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py",    
line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: connection refused

Error in geckodriver.log:

1495299180874   geckodriver::marionette INFO    Starting browser /usr/lib/firefox/firefox with args ["-marionette"]
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :99

Already installed Xvfb and run it:

$ whoami
  codekaizer #with root privileges
$ Xvfb :99 -screen 0 1024x768x24 -ac -fbdir /tmp/.X11-unix/X99 &

Run PHP code snippet for /test endpoint:

$cmd = 'xvfb-run -a python scripts/crawler.py'
return shell_exec($cmd);

Reference for Python code:

#!/usr/bin/env python2

from pyvirtualdisplay import Display
from selenium import webdriver   
import time
import sys

driver = webdriver.Firefox(capabilities={"marionette":True})

display = Display(visible=0, size=(800,600))
display.start()

driver.get('https://www.google.com')
print driver.title
driver.close()
display.stop()

I'm quite stuck right now and really appreciate someone's help!

Details:

Thanks! - ck

Upvotes: 2

Views: 2870

Answers (1)

Levi Noecker
Levi Noecker

Reputation: 3300

You are mixing two different methods for using Xvfb: running it from the command line, and running it from pyvirtualdisplay. The reason the command line approach isn't working is because you aren't connecting your new Xvfb instance to your system display, and the reason the pyvirtualdisplay approach isn't working is because you are attempting to instantiate the browser before pyvirtualdisplay has created a virtual frame buffer for your browser instance to run in. Pick one approach, but don't do both.

If you want to run it from the command line, you must also export your DISPLAY to match the port you set:

Xvfb :99 -screen 0 1024x768x24 -ac -fbdir /tmp/.X11-unix/X99 &
export DISPLAY=:99

python yourscript.py

Or, the better way is to let pyvirtualdisplay manage all of this programatically, like you are almost doing:

#!/usr/bin/env python2

from pyvirtualdisplay import Display
from selenium import webdriver   
import time
import sys

# Use an context manager to handle the start() and stop()
# Instantiate Display BEFORE you try to instantiate the driver
with Display(visible=0, size=(800,600)):
    driver = webdriver.Firefox(capabilities={"marionette":True})

    try:
        driver.get('https://www.google.com')
        print driver.title
    finally:
        # A try/finally block like this insures the driver is closed even if an exception is thrown
        driver.close()

Upvotes: 1

Related Questions