如何通过 Selenium Python 获取元素的坐标或尺寸?


我们可以通过 Selenium 网页驱动获得元素的坐标或尺寸。每个元素都有 .size .location 属性,其中以字典的形式给出了 x、y 坐标以及宽高。

语法 −

loc = element.location

s = element.size

我们来考虑一个元素,我们将找到它的坐标和尺寸−

示例

from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.get("https://tutorialspoint.com/about/about_careers.htm")
#identify element
l= driver.find_element_by_xpath("//img[@class='tp-logo']")
#get x, y coordinates
loc = l.location
#get height, width
s = l.size
print(loc)
print(s)
driver.close()

输出

更新于: 2020 年 9 月 18 日

3 千次+ 观看

开启你的 职业生涯

完成课程,获得认证

开始
广告
© . All rights reserved.