3 回答

TA貢獻1796條經驗 獲得超4個贊
使用 xpath 或 css(首選)來定位所需的按鈕,如下所示。
讓我們考慮下面的示例 html。
<html><head></head><body>
<button class="same_class">follow</button>
<button class="same_class">follow</button>
</body></html>
現在您可以使用 css,nth-of-type如下所示
button.same_class:nth-of-type(2)
CSS 截圖:
您也可以使用 xpath 實現相同的目的。
(//button[@class='same_class'])[2]
XPath 截圖:
change the index in parenthesis as required (index starts with 1
not 0)

TA貢獻1788條經驗 獲得超4個贊
您可以在為元素構造 XPath 時使用該數組。如果 UI 上有兩個具有相同類名的按鈕元素。例如 .//*[@class='XYZ']
.//*[@class='XYZ']
因此,如果上面的 XPath 標識了兩個元素,您可以使用 ( )[1]定位第一個,然后使用 ( .//*[@class='XYZ']
)[2]定位第二個
讓我知道這是否解決了您的查詢。

TA貢獻1876條經驗 獲得超6個贊
假設您<button>
在 DOM 中定義了 2 個標簽,如下所示:
<button?name="foo">foo</button> <button?name="foo">foo</button>
您可以使用 XPath?position()函數匹配您想要的任何按鈕
第一個按鈕:
//button[@name='foo' and position() = 1]
第二個按鈕:
//button[@name='foo' and position() = 2]
使用函數的好處position()
是瀏覽器不再尋找更多的匹配項,因此它不會找到所有按鈕并將它們過濾到所需的按鈕,而是找到具有給定位置的按鈕并停止減少執行時間和資源需求。
添加回答
舉報