'Python Selenium image size

How do I get the size of an image with selenium ?

Finding the image is simple :

driver.find_element_by_tag_name('img')

But I didn't find anywhere how to get its size (not dimensions, size in bytes or kilobytes).



Solution 1:[1]

You can download the image

with open('file.png', 'wb') as file:
    file.write(driver.find_element_by_xpath('path').screenshot_as_png)

and find size

os.path.getsize("filepath")

Solution 2:[2]

size attribute returns the size of the element.

As an example to print the size of the Google Home Page Search Box you can use the following solution:

driver.get("https://www.google.com/")
print(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).size)

Console Output:

{'height': 34, 'width': 487}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BuddyBob
Solution 2 undetected Selenium