Selenium: get coordinates or dimensions of element with Python

Question:

I see that there are methods for getting the screen position and dimensions of an element through various Java libraries for Selenium, such as org.openqa.selenium.Dimension, which offers .getSize(), and org.openqa.selenium.Point with getLocation().

Is there any way to get either the location or dimensions of an element with the Selenium Python bindings?

Asked By: meetar

||

Answers:

Got it! The clue was on selenium.webdriver.remote.webelement — Selenium 3.14 documentation.

WebElements have the properties .size and .location. Both are of type dict.

driver = webdriver.Firefox()

e = driver.find_element_by_xpath("//someXpath")

location = e.location
size = e.size
w, h = size['width'], size['height']

print(location)
print(size)
print(w, h)

Output:

{'y': 202, 'x': 165}
{'width': 77, 'height': 22}
77 22

They also have a property called rect which is itself a dict, and contains the element’s size and location.

Answered By: meetar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.