DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Iptables Basic Commands for Novice
  • Simulate Network Latency and Packet Drop In Linux
  • OpenCV Integration With Live 360 Video for Robotics
  • Use Apache Kafka SASL OAUTHBEARER With Python

Trending

  • Avoiding AI Debt as a Team
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • You Do Not Need a PhD to Become an AI Engineer: The Essential Skill Stack
  • Prompt Caching: Overriding Tokenization for Faster and More Cost-Effective AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Taking Browser Screenshots With No Display (Selenium/Xvfb)

Taking Browser Screenshots With No Display (Selenium/Xvfb)

By 
Corey Goldberg
·
May. 16, 12 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
26.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In my last two blog posts, I showed examples of using Selenium WebDriver to capture screenshots, and running in a headless (no X-server) mode.

This example combines the two solutions to capture screenshots inside a virtual display.

To achieve this, I use a combination of Selenium WebDriver and pyvirtualdisplay (which uses xvfb) to run a browser in a virtual display and capture screenshots.

the setup you need is:

  • Selenium 2 Python bindings: PyPI
  • pyvirtualdisplay Python package (depends on xvfb): PyPI

On Debian/Ubuntu Linux systems, you can install everything with:

$ sudo apt-get install python-pip xvfb xserver-xephyr
$ sudo pip install selenium

once you have it setup, the following code example should work:

#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

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

browser = webdriver.Firefox()
browser.get('http://www.google.com')
browser.save_screenshot('screenie.png')
browser.quit()

display.stop()

this will:

  • launch a virtual display
  • launch Firefox browser inside the virtual display
  • navigate to google.com
  • capture and save a screenshot
  • close the browser
  • stop the virtual display
Python (language) POST (HTTP) Linux (operating system) Binding (linguistics) Blog

Published at DZone with permission of Corey Goldberg. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Iptables Basic Commands for Novice
  • Simulate Network Latency and Packet Drop In Linux
  • OpenCV Integration With Live 360 Video for Robotics
  • Use Apache Kafka SASL OAUTHBEARER With Python

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook