Selenium:使用 Python 获取元素的坐标或尺寸 [英] Selenium: get coordinates or dimensions of element with Python

查看:42
本文介绍了Selenium:使用 Python 获取元素的坐标或尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆