Taking Screenshot

In Previous section, we have discussed about the Headless Chrome and Firefox Browsers and how to run the selenium script in Headless mode and Incognito Mode. Now we will concentrate on how to handle taking Screenshot in Selenium Python.

Objective:

  • Types of Screenshot
  • Taking Screenshot for Visible Area
  • Taking Screenshot for Full page

Types of Screenshot:

Taking Screenshot is very important concept and very often used. There is a scenario where we need to take screenshot of the webpage on every failed test. Here Screenshot will be the crucial role.

There is 2 type of screenshot we can handle in Selenium.

  • Visible Area
  • Full Page

Taking Screenshot for Visible Area:

Visible area in website the area you are seeing in webpage. This is very often used scenario where we need to take screenshot for the issue. To handle this, we need a function called get_screenshot_as_file(). We need to pass the name of the file as parameter. The file will be saved in the current location. Once you open the file you can see the screenshot of the visible area was taken as saved in .png format.

from selenium import webdriver
from webdriver_manager.chromeimport ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

driver.implicitly_wait(10)
driver.get('https://www.reddit.com/')
driver.maximize_window()

driver.get_screenshot_as_file('reddit_1.png')

Output:

Taking Screenshot

There is another method called get_screenshot_as_png(). It will save the screenshot directly as png and name of the file will be the file name of python.

from selenium import webdriver
from webdriver_manager.chromeimport ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

driver.implicitly_wait(10)
driver.get('https://www.reddit.com/')
driver.maximize_window()
driver.get_screenshot_as_png()

Output:

Taking Screenshot for Full Area:

Full area is the area of the whole webpage. Even if you will not see some part of the webpage, you can take screenshot for that also Selenium Python. To achieve this, your script should run in headless mode.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager

options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(ChromeDriverManager().install(), options= options)

driver.implicitly_wait(10)
driver.get('https://www.reddit.com/')
driver.maximize_window()
driver.get_screenshot_as_png()

S = lambda X:driver.execute_script('return document.body.parentNode.scroll' + X)
driver.set_window_size(S('Width'), S('Height'))
driver.find_element(By.TAG_NAME, 'body').screenshot('reddit_full_1.png')

Output:

Summary:

  • Taking Screenshot is very important concept and very often used. There is a scenario where we need to take screenshot of the webpage on every failed test. Here Screenshot will be the crucial role.
  • There is 2 type of screenshot we can handle in Selenium. i.e. Visible Area and Full Area.
  • To handle screenshot, we need a function called get_screenshot_as_file(). We need to pass the name of the file as parameter.
  • There is another method called get_screenshot_as_png(). It will save the screenshot directly as png and name of the file will be the file name of python.
  • Full area is the area of the whole webpage. Even if you will not see some part of the webpage, you can take screenshot for that also Selenium Python. To achieve this, your script should run in headless mode.

Leave a Reply

Your email address will not be published. Required fields are marked *