Challenge - 5 Problems
Element Screenshot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this element-level screenshot code?
Consider the following Selenium Python code snippet that takes a screenshot of a specific element on a webpage. What will be the result of running this code?
Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() browser.get('https://example.com') element = browser.find_element(By.TAG_NAME, 'h1') element.screenshot('header.png') browser.quit() print('Screenshot saved')
Attempts:
2 left
💡 Hint
Remember that element.screenshot() saves the image of the element to the given filename.
✗ Incorrect
The code finds the first
element on the page and saves its screenshot as 'header.png'. Then it prints confirmation. No errors occur if the element exists.
❓ locator
intermediate1:30remaining
Which locator is best to capture an element for screenshot?
You want to take a screenshot of a button with text 'Submit' on a webpage. Which locator is the best choice to find this button uniquely and reliably?
Attempts:
2 left
💡 Hint
Choose the locator that uniquely identifies the button by its visible text.
✗ Incorrect
Using XPath with exact text ensures you get the correct 'Submit' button, avoiding other buttons or elements.
❓ assertion
advanced1:30remaining
Which assertion correctly verifies the element screenshot file exists after saving?
After taking an element-level screenshot saved as 'element.png', which assertion correctly checks the file was created?
Selenium Python
import os # Assume element.screenshot('element.png') was called before
Attempts:
2 left
💡 Hint
Check if the file exists and is a file, not a directory.
✗ Incorrect
os.path.isfile returns true if the path exists and is a file. This confirms the screenshot file was created.
🔧 Debug
advanced2:00remaining
Why does this element screenshot code raise an error?
This Selenium Python code raises an error. Identify the cause.
Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() browser.get('https://example.com') element = browser.find_element(By.ID, 'nonexistent') element.screenshot('elem.png') browser.quit()
Attempts:
2 left
💡 Hint
Check if the element locator matches any element on the page.
✗ Incorrect
The element with ID 'nonexistent' is not found, so find_element raises NoSuchElementException before screenshot is called.
❓ framework
expert3:00remaining
How to integrate element-level screenshot in pytest for failure debugging?
You want to automatically take a screenshot of a specific element when a pytest test fails. Which pytest fixture setup correctly implements this?
Selenium Python
import pytest from selenium import webdriver from selenium.webdriver.common.by import By @pytest.fixture() def browser(): driver = webdriver.Chrome() yield driver driver.quit() @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call, when): # Implementation to capture element screenshot on failure outcome = yield rep = outcome.get_result() if rep.when == 'call' and rep.failed(): driver = item.funcargs['browser'] try: elem = driver.find_element(By.ID, 'target') elem.screenshot('fail_elem.png') except Exception: pass
Attempts:
2 left
💡 Hint
Look for a hook that runs after each test and can detect failure.
✗ Incorrect
pytest_runtest_makereport hook runs after each test phase and can detect failure to trigger screenshot capture.