3 回答

TA貢獻2065條經驗 獲得超14個贊
xpath
在您的代碼中嘗試以下-
driver.find_element_by_xpath('//*[local-name()="svg" and @aria-label="Like"]/parent::span/parent::div/parent::button').click()
希望它能檢測到按鈕。讓我知道結果。

TA貢獻1155條經驗 獲得超0個贊
為什么這些不起作用:
//button[@class='wpO6b ']/descendant::svg[@aria-label='Like']
//button[@class='wpO6b ']//svg[@aria-label='Like']
您選擇svg元素而不是按鈕。您必須將svg部分放在謂詞中。
//button[@class='wpO6b' and descendant::svg[@aria-label='Like']]
class這個幾乎不錯,但是由于元素的屬性中有一個空格,所以button它不起作用??梢允褂胏ontains此處更安全的功能對其進行相應修復:
//button[contains(@class,'wpO6b') and descendant::svg[@aria-label='Like']]
選擇按鈕的另一種方法(使用ancestor軸和更強大的謂詞):
//svg[@aria-label="Like" and @fill="#262626"]/ancestor::button[1][contains(@class,"wpO6b")]
如果您正在處理namespaces,請使用以下表達式:
//*[local-name()='button'][contains(@class,'wpO6b') and descendant::*[local-name()='svg'][@aria-label='Like']]
添加回答
舉報