Automating Browser Tasks without Distraction on Ubuntu
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()
- Python's Global Interpreter Lock (GIL): Understanding the Pros and Cons
- Profitable Freelance Writing: Top Niches & Success Advice
- Unlocking the Potential of Podcasting as a Profitable Online Venture
- Effective Strategies for Prompting AI Language Models
Subscribe to RSS Feed
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