selenium Take a screenshot of a webpage Python Selenium take/save screenshot of webpage


Example

from selenium import webdriver

# Create a new cromedriver
driver = webdriver.Chrome()
# Go to www.google.com
driver.get("https://www.google.com")
# Saves a .png file with name my_screenshot_name to the directory that
# you are running the program from.
screenshot_name = "my_screenshot_name.png"
driver.save_screenshot(screenshot_name)

driver.save_screenshot will return 'true' if the screenshot was taken, and 'false' if it was not. Saving screenshots also works with headless browsers. If you want to save a screenshot in a different directory, just add the filepath (relative to where you are running the code from). For example:

screenshot_name = "screenshots/my_screenshot_name.png"

Will save the screenshot in directory "screenshots" inside the directory that python is being run from.