Selenium Webdriver - Capture Screenshots



We can capture screenshots with the Selenium webdriver using the save_screenshot method. The path of the screenshot captured is passed as a parameter to this method.

The syntax for capturing the screenshot is as follows −

driver.save_screenshot('logo.png')

Here, an image with the name logo.png should get saved within the project.

Code Implementation

The code implementation for capturing the screenshot is as follows −

from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("https://www.tutorialspoint.com/index.htm")
#capture screenshot - tutorialspoint.png within project
driver.save_screenshot('tutorialspoint.png')
#close driver
driver.close()

Output

Tutorialspoint png

The output shows that an image tutorialspoint.png gets created within the project. It contains the captured screenshot.

Advertisements