This Python script captures a screenshot of a given webpage at a specified resolution and aspect ratio. It utilizes Selenium to automate Chrome and take a screenshot at the desired dimensions without cropping or resizing.
- Capture a full-page screenshot at a specified resolution (e.g., 1920x1080 for 16:9).
- Uses headless Chrome for efficient rendering.
- Supports custom resolution input.
- Fully automated with Selenium WebDriver.
You need to install Selenium and Validators:
pip install selenium validatorsEnsure that Google Chrome is installed on your system.
To install ChromeDriver:
-
Find your Chrome version:
- Open Chrome and go to
chrome://settings/help - Note down the version number (e.g., 120.0.0.1)
- Open Chrome and go to
-
Download the corresponding ChromeDriver version from:
-
Extract and place
chromedriverin the project directory or add it to your systemPATH.
echo 'export PATH="$PATH:~/path-to-chromedriver"' >> ~/.zshrc
source ~/.zshrc- Move
chromedriver.exeto a folder (e.g.,C:\chromedriver) - Open System Properties → Advanced → Environment Variables
- Under System Variables, find
Pathand click Edit - Click New and add
C:\chromedriver - Click OK, restart the terminal, and verify with:
chromedriver --versionecho 'export PATH="$PATH:/path-to-chromedriver"' >> ~/.bashrc
source ~/.bashrcpython screenshot.py- Enter the URL of the webpage.
- Specify width (e.g., 1920 for Full HD resolution).
- Specify height (e.g., 1080 for a 16:9 ratio).
Enter URL: https://example.com
Enter width: 1920
Enter height: 1080
The script will generate a screenshot.png file in the current directory.
- Launches headless Chrome with the specified window size.
- Loads the webpage and waits for full rendering.
- Takes a screenshot at the exact resolution specified.
- Saves the screenshot without resizing or cropping.
If you get an error related to ChromeDriver, make sure you have:
- The latest version of Google Chrome installed.
- The correct version of ChromeDriver matching your Chrome version.
Increase the time.sleep(3) value in the script to allow more time for the webpage to load.
This project is licensed under the MIT License.
Enjoy capturing high-quality webpage screenshots! 🚀