No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Webpage Screenshot with Python

by Khaleel O.
June 23, 2022
in Python
Reading Time: 3 mins read
A A
python webpage screenshot
python webpage screenshot

Let’s take a webpage screenshot with Python. We will be using Python 3.8.10 on Windows 10. Let’s go! 🔥⚡✨

Typical use cases for taking a screenshot of a webpage include information gathering and automated functional or user acceptance testing. First you will need to install the relevant library:

pip install selenium

The selenium library is a simple and popular way to implement software testing using the Selenium Webdriver. The webdriver is used to interface with the web browser. In this example we will be using the geckodriver webdriver to interact with Firefox.

To proceed you need to ensure you have the Firefox browser installed, which you can find HERE. Next, you will need to download the webdriver, which you can find in the Github repo HERE. Find, download and unzip the contents of the geckodriver-v0.31.0-win32.zip file in the repo to the same folder as your Python script. This is the driver that will work for this example on Windows 10. Let’s write our code:

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox(executable_path=r'geckodriver.exe')
driver.get('https://wwe.com')
sleep(1)


driver.get_full_page_screenshot_as_file("wwe.png")
#driver.get_screenshot_as_file("wwe.png") #alternative
driver.quit()
print("SCREENSHOT CAUGHT!")

Let’s explain what is happening here:

  1. First we import our libraries. We import selenium and the time libraries.
  2. The method webdriver.Firefox() creates an instance of the Firefox WebDriver. It accepts one parameter which is the geckodriver file we downloaded and unzipped earlier. Recall that this file must exist in the same folder as our Python script for our code to work.
  3. The driver.get() method will navigate to the URL provided as the parameter. In this case we are going to take a screenshot of wwe.com
  4. We will sleep our program for 1 second to give the site time to load.
  5. The driver.get_full_page_screenshot_as_file() will take a full page snapshot of the homepage at the URL we defined in the driver.get() method. It takes one parameter which is the name of the PNG file of the screenshot.
  6. The method driver.quit() quits the driver and closes the browser window.

When the above code executes we will see a short pause, then the Firefox browser window will open automatically, the site will load and then the window will automatically close and the script will terminate. There should then be a new file named wwe.png in the same folder as your python script.

python webpage screenshot
Long isn’t it?

We hope this helped. Happy coding!👌👌👌

Previous Post

Cool Progress Bar in Python

Next Post

Python AES CBC Encrypt Example

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy