How to take screenshot of browser window in Selenium Python?

Selenium Python – Screenshot of Browser Window

In this tutorial, you will learn how to take the screenshot of a browser window in Selenium Python and save it to a specific path.

To take the screenshot of the webpage in browser window in Selenium Python, you can use save_screenshot() method of the webdriver object.

Call save_screenshot() method on the driver object, and pass the location of the path as a string to which we would like to save the screenshot.

driver.save_screenshot("path/to/screenshot.png")

Examples

1. Save screenshot of the webpage

In the following example, we initialize a Chrome webdriver, navigate to a specific URL, take the screenshot, and save the screenshot to a location.

Python Program

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.set_window_size(500, 500)

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-12.html')

# Saving screenshot of the browser
driver.save_screenshot("screenshot.png")

# Close the driver
driver.quit()
Copy

Screenshot

Selenium Python - Screenshot of Browser Window

Please note that the screenshot contains only the contents inside the browser window, but not the window tile bar, etc.

Summary

In this Python Selenium tutorial, we have given instructions on how to take the screenshot of a webpage in the browser window, and save it a to specific location, using save_screenshot() method.

Related Tutorials

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. What is the method used to find web elements in Selenium Python?

Not answered

Q3. Which of the following is not a method to wait for a web element to load in Selenium Python?

Not answered

Q4. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered
Code copied to clipboard successfully 👍