Selenium:使用 Python 获取元素的坐标或尺寸 [英] Selenium: get coordinates or dimensions of element with Python
问题描述
我看到有一些方法可以通过 Selenium 的各种 Java 库获取元素的屏幕位置和尺寸,例如 org.openqa.selenium.Dimension
,它提供了 .getSize()
和 org.openqa.selenium.Point
和 getLocation()
.
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()
.
有没有办法使用 Selenium Python 绑定获取元素的位置或尺寸?
Is there any way to get either the location or dimensions of an element with the Selenium Python bindings?
推荐答案
明白了!线索在 selenium.webdriver.remote.webelement — Selenium 3.14 文档.
WebElements 具有属性 .size
和 .location
.两者都是 dict
类型.
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)
输出:
{'y': 202, 'x': 165}
{'width': 77, 'height': 22}
77 22
他们还有一个名为 rect
本身就是一个 dict
,包含元素的 size
和位置
.
They also have a property called rect
which is itself a dict
, and contains the element's size
and location
.
这篇关于Selenium:使用 Python 获取元素的坐标或尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!