top of page

Take Screen shot on Test Failure Python Selenium Automation Testing

Updated: Jan 28

Take Screen shot on Test Failure

# Import necessary libraries
import time
from selenium import webdriver
from import By

# Create a new instance of the Chrome WebDriver
driver = webdriver.Chrome()

# Open the first tab and navigate to a website

# Find the page title element using its tag name ("h1")
page_title = driver.find_element(By.TAG_NAME, "h1")

# Retrieve the text content of the page title
actual_message = page_title.text

# Highlight the page title element by changing its style temporarily
driver.execute_script("arguments[0].setAttribute('style','background:yellow; border: 2px solid red;');", page_title)

# Pause script execution for 3 seconds for visibility (optional)

# Print the actual page title message

# Define the expected page title message
expected_message = "The Internet"

# Perform assertion to check if actual message matches expected message
if actual_message == expected_message:
    assert True
    # Save a screenshot if assertion fails
    driver.save_screenshot(".\\Screenshots\\" + "title.png")
    assert False

5 views0 comments

Recent Posts

See All

What is Flaky test? Steps to resolve Test Flakiness

A flaky test An automated test in a software testing environment that may produce inconsistent results, sometimes passing and sometimes failing, even when applied to the same version of the software u

Retrieve data from a JSON file

To retrieve data from a JSON file using Java and Selenium, you can use a library like Gson or Jackson for handling JSON parsing. { "username": "Tester1", "password": "Test1234", "url": "https://demoqa

bottom of page