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?
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With