如何做到這一點,以便如果 XPATH 在頁面上看不到該元素,它會跳過該元素并移至下一行?我的代碼是:1. driver.find_element_by_xpath('//img[@alt = "Black"]').click()然后,如果找不到第一個元素,它會跳過并嘗試查找:2. driver.find_element_by_xpath('//*[@id="add-remove-buttons"]/input').click()我猜這與“如果顯示____,單擊,否則如果____單擊”有關,盡管我不確定如何格式化它。
2 回答

慕的地8271018
TA貢獻1796條經驗 獲得超4個贊
使用 try 和 catch 塊并捕獲 ElementNotFoundException 異常 Java:
try{
?//your code to find element
}
catch(ElementNotFoundException)
{
? //print element not found
}
// find another element and perform action
Python:
?try:
? //your code
?except ElementNotFoundException:
? ?print("ELEMENT NOT fOUND")

BIG陽
TA貢獻1859條經驗 獲得超6個贊
在 xpath 中你可以獲得兩個節點集的并集
driver.find_element_by_xpath( '//img[@alt = "Black"]|//*[@id="add-remove-buttons"]/input').click()
添加回答
舉報
0/150
提交
取消