Selenium用のさまざまなJavaライブラリを介して要素の画面位置とサイズを取得する方法があることがわかります。たとえばorg.openqa.selenium.Dimension、を提供し.getSize()、をorg.openqa.selenium.Point使用しgetLocation()ます。
Selenium Pythonバインディングを使用して要素の場所または寸法を取得する方法はありますか?
とった!手がかりは、selenium.webdriver.remote.webelement —Selenium3.14のドキュメントにありました。
WebElementsにはプロパティ.sizeと。があります.location。どちらもタイプ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とを含むというプロパティもありlocationます。