簡體   English   中英

找到最近的Button Selenium Python

[英]Locating nearest Button Selenium Python

找到包含特定主題標簽,哈士奇(超鏈接)的評論后,我試圖單擊“刪除評論”按鈕。

因為有多個“刪除評論”按鈕,所以我認為最好的方法是只找到帶有#標簽的評論,然后單擊最近的按鈕,但是我可能是錯的。

在圖片中,我想單擊主題標簽下方而不是下方的突出顯示的按鈕:

在此處輸入圖片說明

到目前為止,我有

self.browser.find_element_by_xpath('//a[@href="/explore/tags/husky/"]')

哪個可以成功找到標簽,但是在那之后我很困惑。

您可以使用下面的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM