Screenshot of window in Selenium


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('/tmp/selenium/index-12.html')

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

# Close the driver
driver.quit()

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.