Take screenshot of any webpage (URL) using Python
In this tutorial, we are going to learn how to take screenshots of any webpage (URL) using Python.
we are going to take screenshots of any webpage(URL) using the Selenium python library
What is the Selenium Python library?
- Selenium Python library helps us to open URL links using python script.
Installation of selenium library in Python
pip install selenium
Steps how to take a screenshot of any webpage (URL) using Python
1.From this selenium library, we have to import webdriver which helps us to interact with the browser through our system. For this, we have to download some WebDriver.
Download the webdriver using the link given below in your system
Chrome:https://sites.google.com/a/chromium.org/chromedriver/downloads
2. Now copy that path of your WebDriver it is downloaded.
3. Pass that path of the Chrome Web driver to the webdriver.
4. Now we use the “.get()” function to open the URL. And pass the URL in that function.
5. Now we use another inbuilt function “save_screenshot”, This function helps us to take a screenshot of that browser, and save it in our repository, we have to save this screenshot with the extension of “.png”.
from selenium import webdriver path = 'C:\\Users\\sumit\\.wdm\\drivers\\chromedriver\\win32\\102.0.5005.61\\chromedriver.exe' driver= webdriver.Chrome(executable_path=path) driver.get("https://www.google.com") driver.save_screenshot('screenshot.png')
output:
<ipython-input-7-c58d1a7752a8>:3: DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver= webdriver.Chrome(executable_path=path) True
You will get that screenshot at the repository in which you are working means where this current Python file(.py) is saved or opened.
Thus, we have learned how to take screenshots of any webpage (URL) using Python.
Leave a Reply