

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the screenshot of a particular section of the page like the logo of a website in Selenium with python?
We can capture the screenshot of a particular section of the page like the logo of the website in Selenium. For doing so we need to import a PIL imaging library. It may or may not be a part of the standard libraries. However if it is unavailable, it can be installed with pip install Pillow command.
For capturing the screenshot, get_screenshot_as_png() method is available. This method gives a binary data which is present in the memory. Then the image can be modified and finally saved.
There is no built-in method to capture the logo of the web site separately. To achieve this we have to crop the image of the full page to the size of the image logo.
Syntax
driver.get_screenshot_as_png('screenshot_t.png')
In the arguments, we have to provide the screenshot file name along with the extension of .png. If anything else is used as extension, a warning message will be thrown and the image cannot be viewed.
The screenshot gets saved in the same path of the program.
Finally we need to crop the image with the help of location and size methods in Webdriver.
Every element has a unique location measured by the (x, y) co-ordinates. The location method gives two values – x and y coordinates.
Every element has a dimension defined by its height and width. These values are obtained by size method, which gives two values - height and width.
Now for cropping the image.
# to get the axes l = location['x']; t = location['y']; r = location['x']+size['width']; b = location['y']+size['height']; # to compute the cropped image dimension cropImage = Image.open('screenshot_t.png') cropImage = cropImage.crop(l, t, r, b)) cropImage.save('cropImage.png')
Example
Code Implementation for capturing a section of a page.
from selenium import webdriver from PIL import Image from io import BytesIO #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke actual browser driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/about/about_careers.htm") #to refresh the browser driver.refresh() # identifying the logo to capture the screenshot s= driver.find_element_by_xpath("//img[@class='top-logo']") # to get the element location location = s.location # to get the dimension the element size = s.size #to save the screenshot of complete page p = driver.get_screenshot_as_png("logo_tutorialspoint.png") #to get the x axis l = location['x'] #to get the y axis t = location['y'] # to get the length the element b = location['y']+size['height'] # to get the width the element r = location['x']+size['width'] # to open the captured image with PIL imgOpen = Image.open(BytesIO(p)) # to crop the captured image to size of the logo imgLogo = imgLogo.crop(l, t, r, b) # to save the cropped image imgLogo.save("logo_tutorialspoint.png") #to close the browser driver.close()
- Related Questions & Answers
- How to get the screenshot of a particular element in the page in Selenium with python?
- How to get the complete screenshot of a page in Selenium with python?
- Screenshot of a particular element with Python Selenium in Linux
- Take screenshot of full page with Selenium Python with chromedriver.
- How to capture a screenshot of a particular element in Selenium 4.0?
- How to get the values of a particular row in a table in Selenium with python?
- How to count the number of occurrences of a particular text inside a table in a page in Selenium with python?
- How to get the title and URL of the page in Selenium with python?
- How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?
- Best way to take screenshot of a web page into Selenium?
- How to count the number of checkboxes in a page in Selenium with python?
- How to get the total number of checkboxes in a page using Selenium?
- How to check a particular checkbox from a set of checkboxes in a page in Selenium with python?
- How to get the text from a website using selenium?
- How to find the status of an element in a page in Selenium with python?