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!


This post was written by Ramiro Gómez (@yaph) and published on . Subscribe to the Geeksta RSS feed to be informed about new posts.

Tags: automation code linux python selenium tutorial

Disclosure: External links on this website may contain affiliate IDs, which means that I earn a commission if you make a purchase using these links. This allows me to offer hopefully valuable content for free while keeping this website sustainable. For more information, please see the disclosure section on the about page.


Share post: Facebook LinkedIn Reddit Twitter

Merchandise