Selenium:使用 Python 获取元素的坐标或尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15510882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium: get coordinates or dimensions of element with Python
提问by meetar
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.Pointwith getLocation().
我看到有通过各种Java库的硒,如让一个元素的屏幕位置和尺寸的方法org.openqa.selenium.Dimension,提供.getSize()和org.openqa.selenium.Point使用getLocation()。
Is there any way to get either the location or dimensions of an element with the Selenium Python bindings?
有没有办法使用 Selenium Python 绑定获取元素的位置或尺寸?
采纳答案by meetar
Got it! The clue was on selenium.webdriver.remote.webelement — Selenium 3.14 documentation.
知道了!线索在selenium.webdriver.remote.webelement — Selenium 3.14 文档中。
WebElements have the properties .sizeand .location. Both are of type dict.
WebElements 具有属性.size和.location. 两者都是 类型dict。
driver = webdriver.Firefox()
e = driver.find_element_by_xpath("//someXpath")
location = e.location
size = e.size
print(location)
print(size)
Output:
输出:
{'y': 202, 'x': 165}
{'width': 77, 'height': 22}
They also have a property called rectwhich is itself a dict, and contains the element's sizeand location.
它们还有一个名为 a 的属性rect,它dict包含元素的sizeand location。

