In this tutorial, we will introduce how to take a website screenshot using selenium in python. You can learn how to do by following our steps.
Install selenium
You can use pip install command to install selenium.
pip install selenium
Then we can capture a website screenshot using a url.
Import libraries
from PIL import Image from selenium import webdriver
We will create a function to take a website screenshot.
Here is an example:
def getURLScreenshot(url, img): driver = webdriver.Chrome(executable_path=r"E:\Program Files\chromedriver.exe") driver.get(url) flag = False if(driver.save_screenshot(img)): flag = True driver.quit() return flag
We can use getURLScreenshot() function to get a website screenshot.
How to use getURLScreenshot()?
Here is an example:
getURLScreenshot('https://www.tutorialexample.com', 'screenshot.png')
Run this code, you may find this screenshot.
If you want to resize the screenshot image, you can read:
Best Pracice to Python Resize Images with Pillow – Python Tutorial
When you are using python selenium to take a screenshot, you may find some errors, you can read these solutions:
Fix module ‘urllib3’ has no attribute ‘PoolManager’ in Python – Python Tutorial
Fix Selenium ‘chromedriver’ executable needs to be in PATH – Python Tutorial