[英]Select a button with selenium with same XPATH but no tag or id
我想在網上select一個帶有selenium的按鈕,這里是按鈕的HTML代碼:
<button type="button" ng-if="grid.appScope.edit_column" ng-click="grid.appScope.executeActionButtonEvent(row, grid.appScope.edit_column, grid['options'])" class="btn btn-default ng-scope" aria-label="Left Align">
<span class="glyphicon glyphicon-edit glyphicon-align-left" aria-hidden="true"></span>
</button>
它們是 1 到 5,並且具有相同的 XPATH: //html/body/div[2]/div/section/section/div[1]/div/div[2]/div[1]/div[2]/div[2]/div/div[2]/div/div[1]/div/button[1]
我怎樣才能 select 在 HTML 代碼中沒有標簽的第三個或第二個按鈕?
如果這些按鈕值中的ng-click
屬性對於每個元素都是唯一的,您可以使用如下內容:
//button[contains(@ng-click.'grid.appScope.executeActionButtonEvent(row, grid.appScope.edit_column, grid['options'])')]
或grid.appScope.executeActionButtonEvent(row, grid.appScope.edit_column, grid['options'])
值。
如果所有 5 個按鈕元素的這些值都相同,則可以使用以下內容:
(//button[contains(@ng-click.'grid.appScope.executeActionButtonEvent(row, grid.appScope.edit_column, grid['options'])')])[3]
第三個元素。
通常,您可以在此表達式的末尾放置從 1 到 5 的索引,因為它出現在此處。
driver.find_elements_by_xpath("//html/body/div[2]/div/section/section/div[1]/div/div[2]/div[1]/div[2]/div[2]/div/div[2]/div/div[1]/div/button[1]")[2]
您可以使用 find_elements 代替 find_element。 然后就可以得到結果的2.index了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.