如何使用 Selenium Python 获取元素的坐标或尺寸?

seleniumautomation testingtesting tools更新于 2026/2/11 12:52:17

我们可以使用 Selenium webdriver 获取元素的坐标或尺寸。每个元素都具有 .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://www.tutorialspoint.com/about/about_careers.htm")
#identify element
l= driver.find_element_by_xpath("//img[@class='tp-logo']")
#获取 x, y 坐标
loc = l.location
#获取高度、宽度
s = l.size
print(loc)
print(s)
driver.close()

输出


相关文章