Test Overview
This test opens a webpage, finds a specific element, takes a screenshot of only that element, and verifies the screenshot file is created successfully.
This test opens a webpage, finds a specific element, takes a screenshot of only that element, and verifies the screenshot file is created successfully.
from selenium import webdriver from selenium.webdriver.common.by import By import os import time def test_element_screenshot(): driver = webdriver.Chrome() try: driver.get('https://example.com') element = driver.find_element(By.TAG_NAME, 'h1') screenshot_path = 'element_screenshot.png' element.screenshot(screenshot_path) time.sleep(1) # wait to ensure file is saved assert os.path.exists(screenshot_path), 'Screenshot file not found' finally: driver.quit()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open, ready to navigate | - | PASS |
| 2 | Navigates to 'https://example.com' | Browser displays the example.com homepage with visible <h1> element | - | PASS |
| 3 | Finds the <h1> element by tag name | <h1> element is located on the page | Element is found without exception | PASS |
| 4 | Takes screenshot of the <h1> element and saves as 'element_screenshot.png' | Screenshot file is created in the test folder | File 'element_screenshot.png' exists on disk | PASS |
| 5 | Quits the browser and ends the test | Browser window is closed | - | PASS |