How to get the complete screenshot of a page in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

We can get the complete screenshot of a page in Selenium. While executing any test cases, we might encounter failures. To keep track of the failures we capture a screenshot of the web page where the error exists.

In a test case, there may be failure for reasons listed below −

  • If the assertion does not pass.
  • If there are sync issues between our application and Selenium.
  • If there are timeout issues.
  • If an alert appears in between.
  • If the element cannot be identified with the locators.
  • If the actual and final results are not matching.

For capturing the screenshot, save_screenshot() method is available. This method takes the full page screenshot.

Syntax

driver.save_screenshot("screenshot_t.png")

In the arguments, we have to provide the screenshot file name along with the extension of .png. If anything else is used as extension, a warning message will be thrown and the image cannot be viewed.

The screenshot gets saved in the same path of the program.

Example

Code Implementation for full page screenshot.

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then
#invoke actual browser
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.tutorialspoint.com/index.htm")
#to refresh the browser
driver.refresh()
#to get the screenshot of complete page
driver.save_screenshot("screenshot_tutorialspoint.png")
#to close the browser
driver.close()
raja
Published on 29-Jul-2020 14:20:41
Advertisements