Selenium: get coordinates or dimensions of element with Python

PythonSelenium

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmeetarView Question on Stackoverflow
Solution 1 - PythonmeetarView Answer on Stackoverflow