Element-level screenshot in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import os # Initialize the Chrome WebDriver with webdriver.Chrome() as driver: driver.get('https://example.com') try: # Wait up to 10 seconds for the element with id 'logo' to be visible logo_element = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.ID, 'logo')) ) # Take screenshot of the element screenshot_path = os.path.join(os.getcwd(), 'logo_screenshot.png') logo_element.screenshot(screenshot_path) # Assert the screenshot file exists assert os.path.isfile(screenshot_path), f"Screenshot file not found at {screenshot_path}" except Exception as e: print(f"Test failed: {e}") raise
The script starts by importing necessary Selenium modules and os for file path handling.
It opens Chrome browser and navigates to https://example.com.
Using WebDriverWait with visibility_of_element_located, it waits up to 10 seconds for the element with id logo to appear and be visible. This avoids errors if the element loads slowly.
Once found, it calls screenshot() on the element to capture only that element's image, saving it as logo_screenshot.png in the current directory.
Then it asserts the screenshot file exists to confirm success.
The with block ensures the browser closes automatically after the test.
Exceptions are caught and printed to help debug if the element is missing or other errors occur.
Now add data-driven testing to take element screenshots from three different element IDs: 'logo', 'header', and 'footer'.
Practice
element.screenshot('file.png') method do in Selenium Python?Solution
Step 1: Understand the method purpose
Theelement.screenshot()method captures only the visible part of the specific element, not the whole page.Step 2: Confirm the file saving behavior
The method saves the screenshot as the given filename, like 'file.png'.Final Answer:
Takes a screenshot of the specific web element and saves it as 'file.png'. -> Option BQuick Check:
element.screenshot() = element screenshot [OK]
- Confusing element screenshot with full page screenshot
- Thinking it highlights or deletes the element
- Using it without locating the element first
button and save it as 'button.png'?Solution
Step 1: Identify the correct method for element screenshot
Thescreenshot()method is called on the element object, herebutton.Step 2: Confirm the syntax matches the method signature
The correct syntax isbutton.screenshot('button.png'). Other options use incorrect methods or call on driver.Final Answer:
button.screenshot('button.png') -> Option DQuick Check:
element.screenshot(filename) = correct syntax [OK]
- Calling screenshot on driver instead of element
- Using non-existent methods like save_screenshot on element
- Passing element as argument to driver methods
element = driver.find_element(By.ID, 'logo')
element.screenshot('logo.png')What will happen if the element with ID 'logo' is not visible on the page?
Solution
Step 1: Understand element visibility effect on screenshot
If the element is found but not visible, Selenium still takes a screenshot of the element's area, which may be blank.Step 2: Confirm behavior on invisible elements
No exception is raised for invisibility; the file is created but image content is empty or transparent.Final Answer:
The screenshot will be taken but the image will be blank or empty. -> Option AQuick Check:
Invisible element screenshot = blank image [OK]
- Expecting an exception for invisible elements
- Thinking full page screenshot is saved
- Assuming file won't be created
element = driver.find_element(By.CLASS_NAME, 'submit-btn') element.screenshot = 'submit.png'
Solution
Step 1: Check method usage for screenshot
The code assigns a string toelement.screenshot, butscreenshotis a method and must be called with parentheses.Step 2: Confirm correct method call syntax
The correct usage iselement.screenshot('submit.png'), not assignment.Final Answer:
Incorrect assignment; screenshot is a method, not a property. -> Option CQuick Check:
Use parentheses to call screenshot() method [OK]
- Assigning filename to screenshot instead of calling method
- Using wrong locator strategy
- Expecting list when find_element returns single element
Solution
Step 1: Locate multiple elements correctly
Usefind_elementsto get a list of all buttons by tag name 'button'.Step 2: Loop with index and save unique filenames
Useenumeratestarting at 1 to create filenames like 'button1.png', 'button2.png'. Callbtn.screenshot()on each element.Final Answer:
buttons = driver.find_elements(By.TAG_NAME, 'button') for i, btn in enumerate(buttons, 1): btn.screenshot(f'button{i}.png') -> Option AQuick Check:
Loop elements with enumerate and call screenshot() [OK]
- Calling screenshot on list instead of element
- Using find_element instead of find_elements for multiple
- Saving all screenshots with same filename
Ready to prove it?
Pass the test and this pattern moves unknown
