Selenium:使用Python获取元素的坐标或尺寸

mee*_*tar 50 python selenium

我看到有一些方法可以通过Selenium的各种Java库获取元素的屏幕位置和尺寸,例如org.openqa.selenium.Dimension,提供.getSize()org.openqa.selenium.Point使用getLocation().

有没有办法获得Selenium Python绑定元素的位置或尺寸?

mee*_*tar 83

得到它了!线索在https://seleniumhq.github.io/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html上

WebElements具有属性.size.location.两者都是类型dict.

driver = webdriver.Firefox()

e = driver.find_element_by_xpath("//someXpath")

location = e.location
size = e.size

print(location)
print(size)
Run Code Online (Sandbox Code Playgroud)

返回:

{'y': 202, 'x': 165}
{'width': 77, 'height': 22}
Run Code Online (Sandbox Code Playgroud)

它们还有一个称为属性的属性rect,它dict包含元素sizelocation.

  • 这些坐标是什么意思?它们与元素在屏幕上的位置不同,至少在我的机器上是不同的。它们是否与浏览器窗口大小有关? (3认同)