Skip to content
CodeKruPro

Save a Screenshot of One Element

69/80

Save a Screenshot of One Element

element.screenshot writes one element, not the whole viewport. It saves a PNG to the path you give it. Chrome clips the picture to what is on screen, and in the default headless window this button sits at the very bottom edge, so make the window larger first.

Task:

  1. Set the window to 1280 by 900
  2. Open Buttons and save a screenshot of #leftClick to /workspace/element.png
  3. Print whether the file exists and whether its size is greater than 0

Print the lines below exactly.

Expected output:

Element screenshot taken: True
Element screenshot has content: True

About this lesson

Selenium Python element screenshot: capture one button as an image with element.screenshot

Selenium Python AssignmentsAdvancedScreenshots

How it works

A WebElement has its own screenshot(path) method. It crops the image to that element and saves a PNG, returning True. You call it on the element, not the driver. For data in memory, the properties screenshot_as_png and screenshot_as_base64 return bytes and str, with no parentheses.

The crop follows the screen. In the default headless window, 780 by 580, the visible page area is 437 pixels tall, and the left-click button starts at 436. Shot there, the image was 141 pixels wide but only 1 tall, because the rest was below the visible area. The file was valid and not empty, so a size check still passed. The double-click button, higher up, gave a full 170 by 34 image.

Two fixes work. set_window_size takes a width and a height. With 1280 by 900, the page area grew to 757 pixels and the same button came out 141 by 34. It also worked after the page had loaded. Scrolling the element into view gave the same full image.

os.path.exists returns a bool, and os.path.getsize returns the size in bytes.

About the assignment

You enlarge the browser window, save an image of one button, and confirm that the file exists and is not empty.

What you will do

  1. Resize the window to 1280 wide and 900 tall before the capture, so the button is fully on screen.
  2. Open the page, find the left-click button and keep the element.
  3. Call the screenshot method on that element, with a .png path in the workspace folder.
  4. Use os.path.exists for the first check, wrapping the bool in str().
  5. Compare os.path.getsize with zero for the second check, and print both lines.

Common mistakes

  • Leaving off the .png ending

    The file is still written, but Python prints a UserWarning that the name does not match the file type, and viewers may not open it.

  • Reading the size of a missing file

    If nothing was written, os.path.getsize raises FileNotFoundError. Check os.path.exists first.

  • Skipping the window resize

    In the default window the file is only 1 pixel tall, yet os.path.getsize is above zero, so the checks pass on a useless image. Open the PNG, or enlarge the window first.

In a real test suite

Element screenshots suit visual checks of one component, such as a chart or banner. A test saves the image, and a separate tool compares it with a stored baseline. The rest of the page changes too often to compare.

Next the track moves to tables, where one lookup returns a list of cells.

To try one of these calls on any page without a task, open the Online Selenium Python Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

main.py
from selenium import webdriver
from selenium.webdriver.common.by import By
import os

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/Buttons")
    # Set the window to 1280 by 900, then screenshot #leftClick to /workspace/element.png
finally:
    driver.quit()