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.
Comments