Selenium Headless Firefox WebDriver using PyVirtualDisplay


I needed to take screenshots from websites that i crawl and selenium headless webdriver is the right tool for me. I was using wkhtmltoimage untill now but I am running into problems with some websites.

I’ll be using Debian Linux for this without a Desktop environment so i needed to install selenium headless webdriver and I decided to use Firefox as a web browser.

Selenium Headless WebDriver requirements

Since we don’t have a screen to run Firefox we are going to be using Xvfb to simulate a display and run everything in memory.

apt-get install xvfb

Now we are going to install Firefox and few requirements that maybe already installed on your system but here are anyway:

echo -e "\ndeb http://downloads.sourceforge.net/project/ubuntuzilla/mozilla/apt all main" | tee -a /etc/apt/sources.list > /dev/null
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com C1289A29
apt-get update
apt-get install firefox-mozilla-build
apt-get install libdbus-glib-1-2
apt-get install libgtk2.0-0
apt-get install libasound2

Now we need PyVirtualDisplay which is a python wrapper for Xvfb used for easy working with virtual displays in python.

pip install pyvirtualdisplay

Now we are going to install selenium as a final component for our project:

pip install selenium

Now here is a simple python script to take a screenshot from a website:

#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

# Set screen resolution to 1366 x 768 like most 15" laptops
display = Display(visible=0, size=(1366, 768))
display.start()

# now Firefox will run in a virtual display.
browser = webdriver.Firefox()

# Sets the width and height of the current window
browser.set_window_size(1366, 768)

# Open the URL
browser.get('https://www.vionblog.com/')

# set timeouts
browser.set_script_timeout(30)
browser.set_page_load_timeout(30) # seconds

# Take screenshot
browser.save_screenshot('vionblog.png')

# quit browser
browser.quit()

# quit Xvfb display
display.stop()

This will create a file vionblog.png with the screenshot of https://www.vionblog.com front page taken with screen resolution of 1366 x 768

Selenium Headless Screenshot

Hope this helps you in taking screenshots from the websites you need.

Xvfbhttps://en.wikipedia.org/wiki/Xvfb
PyVirtualDisplayhttps://pypi.python.org/pypi/PyVirtualDisplay
Seleniumhttp://www.seleniumhq.org/



Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Advertisement