What if you could see exactly what went wrong the moment your test failed, without lifting a finger?
Why Screenshot on failure in Selenium Python? - Purpose & Use Cases
Imagine you run a test manually on a website and it fails. You try to remember what went wrong, but you forgot to take a picture of the screen at the moment of failure.
Later, you cannot explain the problem clearly to your team because you have no visual proof.
Manually taking screenshots during test failures is slow and easy to forget.
Without screenshots, debugging is like guessing in the dark.
This wastes time and causes frustration for everyone involved.
Automatically capturing a screenshot when a test fails saves the exact moment of error.
This visual evidence helps quickly understand and fix the problem.
It makes debugging faster and communication clearer.
if test_failed: print('Test failed, remember to take screenshot manually')
def on_failure(driver, test_name): driver.save_screenshot(f'{test_name}_failure.png')
It enables fast, clear debugging by preserving the exact screen state at failure.
When a login test fails on a website, the automatic screenshot shows the error message and page state, helping developers fix the bug quickly.
Manual screenshot taking is slow and unreliable.
Automatic screenshots capture failure moments perfectly.
This improves debugging speed and team communication.
Practice
Solution
Step 1: Understand the role of screenshots in testing
Screenshots help testers see what the user saw when the test failed.Step 2: Identify the purpose of capturing screenshots on failure
They provide visual evidence to debug UI problems quickly.Final Answer:
To capture the current state of the web page for debugging -> Option AQuick Check:
Screenshot on failure = capture page state [OK]
- Thinking screenshots speed up tests
- Believing screenshots fix errors automatically
- Confusing screenshots with performance tools
Solution
Step 1: Recall Selenium Python screenshot method
The correct method to save a screenshot issave_screenshot().Step 2: Verify method names and syntax
Onlydriver.save_screenshot('error.png')matches Selenium's API.Final Answer:
driver.save_screenshot('error.png') -> Option AQuick Check:
Correct method name = save_screenshot [OK]
- Using camelCase instead of snake_case
- Using non-existent methods like capture or screenshotSave
- Missing parentheses or quotes
try:
driver.find_element(By.ID, 'submit').click()
except Exception as e:
driver.save_screenshot('fail.png')
print('Screenshot saved on failure')Solution
Step 1: Analyze try-except behavior on element not found
If element is missing, exception triggers except block.Step 2: Check actions in except block
Screenshot is saved and message printed: 'Screenshot saved on failure'.Final Answer:
Screenshot saved on failure -> Option DQuick Check:
Exception triggers screenshot and print [OK]
- Assuming test passes silently on failure
- Confusing syntax errors with runtime exceptions
- Ignoring the print statement inside except
try:
driver.find_element(By.NAME, 'login').click()
except:
driver.save_screenshot(failure.png)Solution
Step 1: Check save_screenshot argument syntax
Filename must be a string in quotes, but here it is unquoted.Step 2: Confirm exception handling and method name
Except syntax and method name are correct; only filename is wrong.Final Answer:
Missing quotes around filename in save_screenshot -> Option CQuick Check:
Filename must be a string in quotes [OK]
- Forgetting quotes around filename string
- Changing method name incorrectly
- Misusing except syntax
Solution
Step 1: Understand when to take screenshots
Screenshots should be taken only on failure, inside except block.Step 2: Use timestamp for unique filenames
Usingtime.time()in filename ensures unique screenshot names.Final Answer:
Use try-except around test steps, call driver.save_screenshot(f"fail_{time.time()}.png") in except -> Option BQuick Check:
Screenshot on failure with timestamp = try-except + formatted filename [OK]
- Taking screenshots before test steps always
- Saving screenshots outside exception handling
- Retrying tests without capturing failure screenshots
Ready to prove it?
Pass the test and this pattern moves unknown
