top of page

Selenium Python- Take Screen shot

How can one capture a screenshot using Selenium in Python?

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

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

# Open the specified website

# Maximize the browser window

# Set an implicit wait of 3 seconds to allow elements to be found

# Pause the script execution for 3 seconds (using time.sleep)

# Locate the username field by ID and enter "standard_user"
driver.find_element(By.ID, "user-name").send_keys("standard_user")

# Locate the password field by ID and enter "secret_sauce"
driver.find_element(By.ID, "password").send_keys("secret_sauce")

# Click the login button using XPath
driver.find_element(By.XPATH, "//input[@id='login-button']").click()

# Pause the script execution for 5 seconds (waiting for the page to load or perform actions)

# Locate the specific element to capture by CSS selector
element_to_capture = driver.find_element(By.CSS_SELECTOR, ".inventory_item:nth-child(6)")

# Take a screenshot of the identified element and save it as screenshot.png"

# Alternatively, take a screenshot of the entire page and save it as "screenshot.png"

# Close the browser window

8 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