Take Screenshot in Selenium Python

Profile picture for user arilio666

To capture a screenshot in Python, utilize the screenshot method supplied by the WebDriver class.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium. web driver.support.UI import WebDriverWait
from selenium. web driver.support import expected_conditions as EC
from selenium. web driver.common.action_chains import ActionChains

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.facebook.com")
driver.maximize_window()
att = driver.find_element(By.XPATH,"//a[normalize-space()='Groups']").get_attribute('title')
print(att)
if att == "Explore our groups.":
   print('Tooltip verified')
else:
   print('Wrong Tooltip Text')
driver.save_screenshot('Luffy.png')
  • We begin by importing the Selenium package's web driver module. 
  • Then, using the Chrome driver, we create a new instance of the WebDriver. 
  • Following that, we travel to a webpage by invoking the get method and giving the URL of the webpage, and then we perform a simple tooltip verification.
  • To take a screenshot, we call the driver's save_screenshot method, giving the appropriate filename and extension (in this example, "Luffy.png"). 
  • The screenshot will be saved in the currently selected working directory.
Take Screenshot in Selenium Python