How to take a screenshot in Selenium WebDriver

Selenium is a cross-platform toolset primarily designed to automate web application testing. Selenium is mainly geared toward functional regression testing. It allows the simulation of user interactions with the browser and comprises the following two main components:

  • Selenium IDE

  • Selenium WebDriver

Selenium WebDriver allows taking a screenshot of a chosen web page opened in an automated browser.

The captured screenshot is generated in PNG format and stored in the project directory by default. We may specify another path for the spawned screenshot.

This feature allows capturing screenshots from one or multiple websites and archiving them periodically. The captured screenshots maintain an up-to-date repository of the website information, for example, to monitor the price changes for a given product.

The following two methods are useful when taking screenshots using Selenium WebDriver:

  • save_screenshot

  • get_screenshot_as_file

Let's look at the following examples to understand these methods better:

The save_screenshot method

This method accepts as a parameter the name or the desired path to capture the screenshot.

import os,time,logging
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
os.environ['WDM_PROGRESS_BAR'] = str(0)
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--no-sandbox')
chromeOptions.add_argument('--disable-gpu')
chromeOptions.add_argument('--headless')
chromeOptions.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), chrome_options=chromeOptions)
driver.get('https://www.educative.io/');
output_path = '/usercode/output'
driver.save_screenshot(os.path.join(output_path,'screenshot.png'))
time.sleep(10);
driver.close();

Let's explain the code widget above:

  • Lines 1–5: We'll import the required Python libraries.

  • Line 7: We'll suppress the WebDriver default logging.

  • Lines 9–14: We'll initialize the Chrome WebDriver.

  • Line 16: We'll launch the browser and fetch the specified URL.

  • Line 18: We'll capture and save a screenshot of the loaded page.

  • Line 20: We'll close the browser window opened by the Selenium WebDriver.

The get_screenshot_as_file method

This method yields True if a screenshot is created successfully. Otherwise, it returns False.

import os,time,logging
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
os.environ['WDM_PROGRESS_BAR'] = str(0)
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--no-sandbox')
chromeOptions.add_argument('--disable-gpu')
chromeOptions.add_argument('--headless')
chromeOptions.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), chrome_options=chromeOptions)
driver.get('https://www.educative.io/profile/view/6592891075428352/');
output_path = '/usercode/output'
driver.get_screenshot_as_file(os.path.join(output_path,'screenshot.png'))
time.sleep(10);
driver.close();

The code widget above is similar to the one previously elaborated, except for the following:

  • Line 16: This the fetched URL.

  • Line 18: We'll invoke the get_screenshot_as_file method to grab and create a screenshot of the designated page once loaded.

Combining both methods

This example shows how to combine the previously elaborated methods together.

import os,time,logging
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
os.environ['WDM_PROGRESS_BAR'] = str(0)
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--no-sandbox')
chromeOptions.add_argument('--disable-gpu')
chromeOptions.add_argument('--headless')
chromeOptions.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), chrome_options=chromeOptions)
output_path = '/usercode/output'
driver.get('https://www.educative.io/');
driver.save_screenshot(os.path.join(output_path,'screenshot1.png'))
time.sleep(10);
driver.get('https://www.educative.io/profile/view/6592891075428352/');
driver.get_screenshot_as_file(os.path.join(output_path,'screenshot2.png'))
time.sleep(10);
driver.close();

The code widget above is similar to the ones previously elaborated. The only difference is that we invoked both methods sequentially.

Copyright ©2024 Educative, Inc. All rights reserved