I have a python method that sets up a browser in headless-mode on a linux server for website scraping with selenium. The display gets setup perfectly fine regardless of which user executes the python script but if the sudo
user doesn't execute the script it will hang at the webdriver.Firefox()
setup line indefinitely.
Here is the full method:
def browserSetup(self, browser=None):
try:
# now Firefox will run in a virtual display. you will not see the browser.
self.display = Display(visible=0, size=(800, 600))
self.display.start()
if self.verbose:
print "Virtual display started for browser instantiation."
#change user agent
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1")
profile.set_preference("webdriver.log.file", "webdriver.log")
# Create a new instance of the Firefox driver
browser = webdriver.Firefox(profile)
if self.verbose:
print "Browser window object established @ %s." % browser
return browser
except Exception, e:
raise e
So, to repeat my issue: if the script is not executed as sudo
then the script will hang indefinitely at the webdriver.Firefox
creation line. Why would this be happening?
UPDATE: The problem is this line here:
browser = webdriver.Firefox() #with or without the profile variable - the results are the same
UPDATE AGAIN
Several people in the comments below have suggested I try running Firefox
from the command line manually to see if there are any issues; here are the results:
#initialize the virtual display
$ sudo Xvfb :10 -extension RANDR
[dix] Could not init font path element /usr/share/fonts/X11/cyrillic, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/100dpi/:unscaled, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/75dpi/:unscaled, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/Type1, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/100dpi, removing from list!
[dix] Could not init font path element /usr/share/fonts/X11/75dpi, removing from list!
[dix] Could not init font path element /var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType, removing from list!
#now start firefox in another ssh window (since the Xvfb process is consuming my prompt)
$ export DISPLAY=:10
$ firefox
(firefox:6347): GConf-WARNING **: Client failed to connect to the D-BUS daemon:
//bin/dbus-launch terminated abnormally without any error message
The last error message displays hundreds of times...