如何透過 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.tw/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()
輸出
廣告