How to get image size in Selenium Python?

Selenium Python – Get image size

In this tutorial, you shall learn how to get the size of a given image element, using Selenium in Python language.

To get the size of a given image element in Selenium Python, you can read the size property of the image WebElement object. size property returns a dictionary with ‘width’ and ‘height’ as keys, and the element’s width and height as respective values in pixels.

image.size

An example for the return value of the above expression would be

{'height': 228, 'width': 300}

We can access the height and width of the image separately using keys of this dictionary.

size = image.size
width  = size['width']
height = size['height']

If the image is hidden, size property could throw ElementNotInteractableException. You may use try-except to handle this exception.

try:
    print(image.size)
except ElementNotInteractableException:
    print('image could be hidden')

Examples

In the following examples, we shall consider loading the HTML file at path https://pythonexamples.org/tmp/selenium/index-30.html . The contents of this HTML file is given below.

<html>
<body> 
    <h3>Hello World</h3> 
    <img id="image1" src="https://pythonexamples.org/wp-content/uploads/2023/07/pe4-2.png" alt="My sample image 1">
    <img id="image2" src="https://pythonexamples.org/wp-content/uploads/2023/06/If.png" alt="My sample image 2">
    <img id="image3" src="https://pythonexamples.org/wp-content/uploads/2023/04/test_image-1.jpg" alt="My sample image 3" hidden>
</body>
</html>

1. Get the size of image whose id=”image1″

In the following program, we initialize a driver object, load the specified URL, find the image element with id=”image1″, and get the size of this image element, and print its width and height.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.common.exceptions import ElementNotInteractableException

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-30.html')

# Find the image
image_1 = driver.find_element(By.ID, 'image1')

# Get image size
try:
    size = image_1.size
    print(size)
    # Print width and height separately
    width  = size['width']
    height = size['height']
    print(f'Width : {width}')
    print(f'Height : {height}')
except ElementNotInteractableException:
    print('image could be hidden')

# Close the driver
driver.quit()

Output

{'height': 228, 'width': 300}
Width : 300
Height : 228

2. Print size of all the images

In the following program, we initialize a driver object, load the specified URL, find all the image elements, iterate over the image, and print the size of each image to standard output.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.common.exceptions import ElementNotInteractableException


# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-30.html')

# Find all the images
images = driver.find_elements(By.TAG_NAME, 'img')

# Print size of all the images
for image in images:
    try:
        print(image.size)
    except ElementNotInteractableException:
        print('image could be hidden')

# Close the driver
driver.quit()

Output

{'height': 228, 'width': 300}
{'height': 989, 'width': 900}
image could be hidden

There are three images in the given webpage. The first two images are visible, and the third image is hidden.

The size of the visible image are printed to the standard output. And the size of the hidden image thrown ElementNotInteractableException.

Summary

In this Python Selenium tutorial, we have seen how to get the size of an image element in a webpage, with example programs.

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us