Running Firefox with Selenium on Headless Linux for Automated Operations in Python

Python
2015-08-11 02:06 (10 years ago)
Running Firefox with Selenium on Headless Linux for Automated Operations in Python

This is a method to launch Selenium from Python on a headless Linux system.

Prerequisites

$ sudo apt-get install xvfb

$ pip install selenium

$ pip install pyvirtualdisplay

Instead of a physical display, we use a virtual framebuffer called xvfb.

Pyvirtualdisplay makes it easy to use xvfb from Python.

Python code

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600)) display.start()

fx = webdriver.Firefox() fx.get("http://example.com") fx.save_screenshot("/tmp/webdriver-firefox-screenshot.png")

fx.close() fx.quit() display.stop()

We open http://example.com with Firefox and save a screenshot.

If the termination process is not handled properly, xvfb and Firefox might become zombie processes. In such cases, you can terminate them using killall.

Check if the processes are still running

$ ps -eafw|grep -i "firefox"
$ ps -eafw|grep "xvfb"

Moreover, if you want to develop with a display on a Mac and run without a display on Linux, you can use

import platform
use_virtual_display = platform.system() == 'Linux'

By comparing with platform.system(), you can achieve this relatively easily.

Current rating: 3.0 (2)
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Categories

Archive