79

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?

meetar
  • 7,443
  • 8
  • 42
  • 73

1 Answers1

137

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.

Velkan
  • 7,067
  • 6
  • 43
  • 87
meetar
  • 7,443
  • 8
  • 42
  • 73
  • How to achieve this in C#? I want to get the bounds of an element and then do some calculation and click using x, y coordinates on that element. – rahoolm Aug 12 '14 at 08:03
  • @rahoolm you can just do `e.click()` and selenium will click on it. – if __name__ is None May 14 '16 at 22:59
  • 12
    What do these coordinates mean? They are different from the location of the element on the screen, at least on my machine. Are they relative to the browser window size? – Hristo Vrigazov Aug 11 '16 at 07:58
  • Awesome. Exactly what i need, in my case i can't click in a checkbox usign Chrome headless mode, so i'm trying to figure it whats happening and whats the actual position of the element.. – rodrigorf Mar 15 '18 at 18:54
  • 1
    How can I extract x and y coordinates from this (element.location())? I need the x and y coordinates indivisually. – ePandit Apr 29 '20 at 08:03
  • @ePandit , the returned object is a dictionary in all language bindings, as the answer clearly states, just access values as you normally do for dictionary/collection classes natively. Note that the actual location is going to be relative, you may want to inspect the entire DOM just using the browser "inspect" popup menu. –  May 06 '20 at 12:15