Selenium Python Get Width and Height of Element

Profile picture for user arilio666

The size attribute of the WebElement object can be used to get the width and height of an element when using Selenium in Python.

from selenium import webdriver
from import ChromeDriverManager
from import By
from import Service
import urllib.request

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

downloadUrl = driver.find_element(By.ID,"download")
width = downloadUrl.size['width']
height = downloadUrl.size['height']
print(f'Width: {width}, Height: {height}')
  • Chrome() is a method. In this example, we first use the webdriver to create an instance of the WebDriver. 
  • The method get() is then used to navigate a web page.
  • Then, we use the find_element method to find the element we're looking for, sending in the ID of the element we're looking for.
  • Finally, we use the size attribute of the WebElement object to determine the element's size, and we store the width and height in separate variables. 
  • The values of these variables with the width and height are then printed out.
  • Because the size attribute produces a dictionary containing the width and height, we use the keys 'width' and 'height' to access these values.