[英]Locating nearest Button Selenium Python
您可以使用下面的xpath之一。
說明 :找到一個與“#hasky” 文本 ,得到了“菜單項” 的作用第一父李並獲得子按鈕 (用“刪除注釋” title屬性):
//a[.='#husky']/ancestor::li[@role='menuitem'][1]//button
//a[.='#husky']/ancestor::li[@role='menuitem'][1]//button[@title='Delete Comment']
//a[contains(@href, "/explore/tags/husky/")]/ancestor::li[@role='menuitem'][1]//button
//li[@role='menuitem' and .//a[.='#husky']]//button[@title='Delete Comment']
簡單的東西
//a[.='#husky']//following::button[@title='Delete Comment'][1]
應該工作正常。 如果是我,則將其包裝在方法中,並傳遞鏈接文本以刪除適當的注釋。 然后,您可以獲取鏈接文本,並將其放入#husky
位置的定位器中。
def delete_comment(comment)
driver.find_element_by_xpath(f"//a[.='{comment}']//following::button[@title='Delete Comment'][1]").click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.