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
- Resize the window to 1280 wide and 900 tall before the capture, so the button is fully on screen.
- Open the page, find the left-click button and keep the element.
- Call the screenshot method on that element, with a
.pngpath in the workspace folder. - Use
os.path.existsfor the first check, wrapping theboolinstr(). - Compare
os.path.getsizewith 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
UserWarningthat 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.getsizeraisesFileNotFoundError. Checkos.path.existsfirst.Skipping the window resize
In the default window the file is only 1 pixel tall, yet
os.path.getsizeis 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.
Related lessons
- Save a Screenshot of the ViewportSaves the whole viewport instead of one element.
- Set the Window SizeSet the window size, which decides how much of the page is visible.
- Scroll an Element into ViewScroll an element into view before capturing it.
- Click a Button and Read the New TextWork with the same left-click button on the Buttons page.
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.
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()