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()
Funny programmer shirts offered on Amazon.
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.