Element 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 WebDriver with webdriver.Chrome() as driver: driver.get('https://example.com') # Wait for the element with id 'logo' to be visible wait = WebDriverWait(driver, 10) element = wait.until(EC.visibility_of_element_located((By.ID, 'logo'))) # Take screenshot of the element screenshot_path = os.path.join(os.getcwd(), 'logo_element.png') element.screenshot(screenshot_path) # Assert the screenshot file exists assert os.path.isfile(screenshot_path), f"Screenshot file {screenshot_path} was not created"
This script uses Selenium WebDriver with Python to automate the task.
First, it opens the Chrome browser and navigates to 'https://example.com'.
It waits explicitly up to 10 seconds for the element with id 'logo' to be visible on the page, ensuring the element is ready for interaction.
Once found, it captures a screenshot of only that element and saves it as 'logo_element.png' in the current working directory.
Finally, it asserts that the screenshot file was created successfully.
The browser is automatically closed after the with block ends.
Now add data-driven testing to capture screenshots of elements with ids 'logo', 'header', and 'footer' saving each with the respective name.
Practice
element.screenshot('file.png') method do in Selenium Python?Solution
Step 1: Understand the method purpose
Theelement.screenshot()method captures an image of the specific element, not the whole page.Step 2: Compare with other screenshot methods
driver.save_screenshot()captures the full page, butelement.screenshot()targets only one element.Final Answer:
Captures a screenshot of only the specific web element. -> Option AQuick Check:
element.screenshot() = element image capture [OK]
- Confusing element screenshot with full page screenshot
- Thinking it saves HTML instead of image
- Using driver.screenshot instead of element.screenshot
button as 'button.png'?Solution
Step 1: Identify the correct method for element screenshot
The method to save an element's screenshot iselement.screenshot(filename).Step 2: Check the variable and filename usage
Since the element is stored inbutton, callingbutton.screenshot('button.png')is correct syntax.Final Answer:
button.screenshot('button.png') -> Option CQuick Check:
element.screenshot(filename) = correct syntax [OK]
- Using driver.screenshot() for element instead of element.screenshot()
- Using non-existent methods like save_screenshot on element
- Wrong method name like capture()
element = driver.find_element(By.ID, 'logo')
element.screenshot('logo.png')
print('Screenshot saved')Solution
Step 1: Locate element and call screenshot
The code finds the element with ID 'logo' and callselement.screenshot('logo.png'), which saves the element's image.Step 2: Confirm output message
After saving, it prints 'Screenshot saved' confirming the action.Final Answer:
Saves a screenshot of the element with ID 'logo' and prints confirmation. -> Option BQuick Check:
element.screenshot() saves image + print confirms [OK]
- Assuming screenshot method is invalid for elements
- Thinking it saves full page instead of element
- Ignoring the print statement output
element = driver.find_element(By.CLASS_NAME, 'submit-btn') element.screenshot()
Solution
Step 1: Check screenshot method usage
Thescreenshot()method requires a filename argument to save the image.Step 2: Identify missing filename
Since no filename is given, no file is created, causing the issue.Final Answer:
Missing filename argument in screenshot() method. -> Option DQuick Check:
screenshot() needs filename to save file [OK]
- Calling screenshot() without filename argument
- Assuming locator type is wrong without checking
- Thinking screenshot() method is invalid for elements
Solution
Step 1: Identify the goal - visual verification of a button
To check how the button looks, capturing its image directly is best.Step 2: Choose the method that captures only the button
element.screenshot()saves the button's image exactly, making comparison easy and automated.Final Answer:
Locate the button element and use element.screenshot('button.png') to save its image for comparison. -> Option AQuick Check:
element.screenshot() = best for visual element checks [OK]
- Capturing full page screenshot and cropping manually
- Comparing HTML text instead of visual appearance
- Not saving any image for visual verification
Ready to prove it?
Pass the test and this pattern moves unknown
