Automating Browser Tasks without Distraction on Ubuntu

I use Selenium from Python for automating browser tasks that require rendering and execution of JavaScript code. Usually, I don't want to get distracted by browser windows popping up and therefore use the PhantomJS Webdriver with Selenium.

Unfortunately, PhantomJS stopped working for me today on Ubuntu crashing with a segmentation fault. Fortunately though, it is fairly easy to run the Firefox Webdriver without displaying a browser window on Ubuntu with the help of some additional packages.

First, I had to install Xvfb: a display server implementing the X11 display server protocol, that performs all graphical operations in memory without showing any screen output.:

sudo apt-get install xvfb

Next in the virtual environment I use for automated browser tasks, I installed the Python package PyVirtualDisplay:

pip install pyvirtualdisplay

Finally, I added a few lines of code at the top of my Python script, so the Firefox Webdriver will be used without displaying a browser window as a fallback when PhantomJS crashes.

from selenium import webdriver from pyvirtualdisplay import Display try: browser = webdriver.PhantomJS() except: display = Display(visible=0, size=(1024, 768)) display.start() browser = webdriver.Firefox()

Thanks to Philipp Hagemeister for suggestion PyVirtualDisplay on Stack Overflow!