Skip to content

How to Take Screenshots with Selenium

Selenium is a popular browser automation tool used for web scraping, testing, and more. One useful feature it provides is the ability to take screenshots of web pages during automation. Here‘s an in-depth look at how to take full page and element-level screenshots with Selenium in Python.

Introduction to Taking Screenshots with Selenium

The main reasons you may want to take screenshots with Selenium include:

  • Saving copies of web pages you have scraped or interacted with for debugging or records
  • Capturing dynamic content like date/time info or ads for later analysis
  • Checking layout issues across browsers and devices
  • Documenting workflow steps and states in automated tests
  • Taking screenshots of captcha images to solve them with automation

Selenium supports taking screenshots of both full web pages and specific elements on a page. This provides a lot of flexibility to capture exactly what you need.

Setting Up Selenium and Opening a Browser

To get started, we first need to import Selenium and instantiate a WebDriver object to control the browser. The example below shows opening Chrome:

from selenium import webdriver

driver = webdriver.Chrome()

Other browsers like Firefox and Edge work similarly. With the driver object ready, we can now navigate to a webpage to screenshot:


Taking Full Page Screenshots

The easiest way to take a screenshot of the entire browser window contents is using save_screenshot().

Pass it a file path to save the image directly to disk:


Alternatively, you can get the screenshot as in-memory PNG bytes or a Base64 encoded string:

screenshot_png = driver.get_screenshot_as_png()
screenshot_b64 = driver.get_screenshot_as_base64() 

This allows saving the screenshot to a database or sending it over HTTP, for example.

Taking Element Screenshots

To take screenshots of a specific element on the page rather than the full screen, you first need to find the element using a locator like CSS selector or XPath:

element = driver.find_element(By.ID, ‘main-content‘)

Then call .screenshot() on the WebElement to capture only that piece of the page:

element_shot = element.screenshot(‘element.png‘)

Just like with full screenshots, you can get the bytes or Base64 string as well.

Tips for Reliable Screenshots

Here are some tips for taking accurate, useful screenshots with Selenium:

  • Wait for pages to fully load before taking screenshots to avoid capturing mid-load states.
  • Scroll elements into view before taking screenshots if needed.
  • Watch for ads and dynamic content changing between shots.
  • Take mobile screenshots by resizing browser window or using mobile emulation.
  • Headless browsers like PhantomJS can screenshot but may miss some content.

Use Cases for Selenium Screenshots

Some examples of how you could use Selenium screenshots in your web scraping and automation projects:

  • Debugging and logging – compare before and after screenshots to see changes.
  • Capturing generated or time-sensitive content like dashboards.
  • Automated visual regression testing across browser versions.
  • Documenting workflow and state during test runs.
  • Screenshotting captchas to feed into a solving service.
  • Archiving scraped page data along with screenshots.

Recap and Next Steps

The steps to take screenshots with Selenium in Python are:

  1. Import Selenium and create a WebDriver
  2. Navigate to the target webpage
  3. Call save_screenshot() to capture full page
  4. Or find an element and call screenshot() to capture only that element
  5. Tweak as needed for dynamic pages and mobile

I hope this guide gives you a good foundation for taking screenshots in your own Selenium project! The possibilities are endless.

Join the conversation

Your email address will not be published. Required fields are marked *