Bird
Raised Fist0
Selenium Pythontesting~20 mins

Element-level screenshot in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Element Screenshot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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')
AScreenshot saved (and a file named 'header.png' is created with the element's image)
BNo file is created; code runs without error and prints 'Screenshot saved'
CRaises NoSuchElementException because 'h1' tag does not exist
DSyntaxError due to incorrect method usage
Attempts:
2 left
💡 Hint
Remember that element.screenshot() saves the image of the element to the given filename.
❓ locator
intermediate
1: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?
ABy.XPATH, "//button[text()='Submit']" - finds button by exact text
BBy.CLASS_NAME, 'btn' - finds any element with class 'btn'
CBy.TAG_NAME, 'button' - finds the first button element
DBy.CSS_SELECTOR, 'div > button' - finds any button inside a div
Attempts:
2 left
💡 Hint
Choose the locator that uniquely identifies the button by its visible text.
❓ assertion
advanced
1: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
Aassert os.path.isfile('element.png') == false
Bassert os.path.exists('element.png') == false
Cassert os.path.isfile('element.png')
Dassert os.path.isdir('element.png')
Attempts:
2 left
💡 Hint
Check if the file exists and is a file, not a directory.
🔧 Debug
advanced
2: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()
ATimeoutException because page did not load
BNoSuchElementException because element with ID 'nonexistent' does not exist
CTypeError because screenshot method requires a file object, not a string
DWebDriverException due to invalid browser driver
Attempts:
2 left
💡 Hint
Check if the element locator matches any element on the page.
❓ framework
expert
3: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
AUse pytest.mark.parametrize to pass screenshot filename to tests
BUse pytest.skip to skip tests that fail to capture screenshots
CUse pytest.fixture to capture screenshot after each test regardless of result
DUse pytest_runtest_makereport hook to check test failure and capture element screenshot
Attempts:
2 left
💡 Hint
Look for a hook that runs after each test and can detect failure.

Practice

(1/5)
1. What does the element.screenshot('file.png') method do in Selenium Python?
easy
A. Takes a screenshot of the entire webpage and saves it as 'file.png'.
B. Takes a screenshot of the specific web element and saves it as 'file.png'.
C. Locates the element and highlights it on the page.
D. Deletes the element from the webpage.

Solution

  1. Step 1: Understand the method purpose

    The element.screenshot() method captures only the visible part of the specific element, not the whole page.
  2. Step 2: Confirm the file saving behavior

    The method saves the screenshot as the given filename, like 'file.png'.
  3. Final Answer:

    Takes a screenshot of the specific web element and saves it as 'file.png'. -> Option B
  4. Quick Check:

    element.screenshot() = element screenshot [OK]
Hint: Remember: element.screenshot saves only that element's image [OK]
Common Mistakes:
  • Confusing element screenshot with full page screenshot
  • Thinking it highlights or deletes the element
  • Using it without locating the element first
2. Which of the following is the correct syntax to take a screenshot of a web element stored in variable button and save it as 'button.png'?
easy
A. button.save_screenshot('button.png')
B. driver.screenshot('button.png')
C. driver.save_element_screenshot(button, 'button.png')
D. button.screenshot('button.png')

Solution

  1. Step 1: Identify the correct method for element screenshot

    The screenshot() method is called on the element object, here button.
  2. Step 2: Confirm the syntax matches the method signature

    The correct syntax is button.screenshot('button.png'). Other options use incorrect methods or call on driver.
  3. Final Answer:

    button.screenshot('button.png') -> Option D
  4. Quick Check:

    element.screenshot(filename) = correct syntax [OK]
Hint: Call screenshot() on element, not driver [OK]
Common Mistakes:
  • Calling screenshot on driver instead of element
  • Using non-existent methods like save_screenshot on element
  • Passing element as argument to driver methods
3. Given the code snippet:
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?
medium
A. The screenshot will be taken but the image will be blank or empty.
B. An exception will be raised because the element is not visible.
C. The entire page screenshot will be saved instead.
D. The file 'logo.png' will not be created.

Solution

  1. 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.
  2. Step 2: Confirm behavior on invisible elements

    No exception is raised for invisibility; the file is created but image content is empty or transparent.
  3. Final Answer:

    The screenshot will be taken but the image will be blank or empty. -> Option A
  4. Quick Check:

    Invisible element screenshot = blank image [OK]
Hint: Invisible element screenshot saves blank image, no error [OK]
Common Mistakes:
  • Expecting an exception for invisible elements
  • Thinking full page screenshot is saved
  • Assuming file won't be created
4. Identify the error in this code snippet:
element = driver.find_element(By.CLASS_NAME, 'submit-btn')
element.screenshot = 'submit.png'
medium
A. The element variable should be a list, not a single element.
B. The locator By.CLASS_NAME is invalid.
C. Incorrect assignment; screenshot is a method, not a property.
D. The filename must be a full path, not just 'submit.png'.

Solution

  1. Step 1: Check method usage for screenshot

    The code assigns a string to element.screenshot, but screenshot is a method and must be called with parentheses.
  2. Step 2: Confirm correct method call syntax

    The correct usage is element.screenshot('submit.png'), not assignment.
  3. Final Answer:

    Incorrect assignment; screenshot is a method, not a property. -> Option C
  4. Quick Check:

    Use parentheses to call screenshot() method [OK]
Hint: Remember: screenshot is a method, call with () [OK]
Common Mistakes:
  • Assigning filename to screenshot instead of calling method
  • Using wrong locator strategy
  • Expecting list when find_element returns single element
5. You want to take screenshots of multiple buttons on a page and save them as 'button1.png', 'button2.png', etc. Which code snippet correctly does this?
hard
A. buttons = driver.find_elements(By.TAG_NAME, 'button') for i, btn in enumerate(buttons, 1): btn.screenshot(f'button{i}.png')
B. buttons = driver.find_elements(By.TAG_NAME, 'button') for i in range(len(buttons)): buttons.screenshot(f'button{i}.png')
C. buttons = driver.find_element(By.TAG_NAME, 'button') for i, btn in enumerate(buttons): btn.screenshot('button.png')
D. buttons = driver.find_elements(By.CLASS_NAME, 'button') for btn in buttons: btn.screenshot('button.png')

Solution

  1. Step 1: Locate multiple elements correctly

    Use find_elements to get a list of all buttons by tag name 'button'.
  2. Step 2: Loop with index and save unique filenames

    Use enumerate starting at 1 to create filenames like 'button1.png', 'button2.png'. Call btn.screenshot() on each element.
  3. Final Answer:

    buttons = driver.find_elements(By.TAG_NAME, 'button') for i, btn in enumerate(buttons, 1): btn.screenshot(f'button{i}.png') -> Option A
  4. Quick Check:

    Loop elements with enumerate and call screenshot() [OK]
Hint: Use enumerate to name files uniquely in loop [OK]
Common Mistakes:
  • Calling screenshot on list instead of element
  • Using find_element instead of find_elements for multiple
  • Saving all screenshots with same filename
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard