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
Over 2 million developers have joined DZone.
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

  • AI Agents in Java: Architecting Intelligent Health Data Systems
  • Understanding MCP Architecture: LLM + API vs Model Context Protocol
  • Invisible Failures in S/4HANA Conversions (And Why Teams Miss Them)
  • What Is Plagiarism? How to Avoid It and Cite Sources
  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 user avatar
Corey Goldberg
·
May. 16, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
25.5K Views

Join the DZone community and get the full member experience.

Join For Free

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

The likes didn't load as expected. Please refresh the page and try again.

  • 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