Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: get coordinates or dimensions of element with Python

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?

like image 648
meetar Avatar asked Mar 19 '13 21:03

meetar


People also ask

How would you get the coordinates of an element in Selenium?

Selenium executes JavaScript commands with the help of the executeScript method. To get the unique coordinates of an element we shall create an object of the class Point which shall store the location of the webelement obtained from the getLocation method.

How do you find the height and width of an element in Selenium?

To get the width and height of a rendered web element programmatically using Selenium in Java, use WebElement. getSize() function. We first find the web element by name, id, class name, etc., and then call the getSize() function on this web element. getSize() function returns a Dimension object.

How do you find the attributes of an element in Python?

get_attribute method is used to get attributes of an element, such as getting href attribute of anchor tag. This method will first try to return the value of a property with the given name. If a property with that name doesn't exist, it returns the value of the attribute with the same name.

How do you find height width color and font of an element using Selenium?

For getting CSS value: driver. findElement(By.id("by-id")). getCssValue("font-size");//similarly you can use other CSS property such as background-color, font-family etc.


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

like image 65
meetar Avatar answered Oct 01 '22 21:10

meetar