Home > Software engineering >  Python Selenium image size
Python Selenium image size

Time:05-08

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).

CodePudding user response:

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")

CodePudding user response:

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}
  • Related