平臺上至少還有 2 個問題,但他們的回答都沒有幫助我。
我剛匯入:
from selenium.common.exceptions import NoSuchElementException
然后使用:
if Newest_tab_button:
print('element found')
else:
print('not found')
或者
try:
wait = WebDriverWait(driver, 15)
Newest_tab_button = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@text="NEWEST"]//ancestor::*[contains(@resource-id, "itemContainer")]')))
except NoSuchElementException:
print('Element not found')
沒有任何效果,仍然得到:
selenium.common.exceptions.TimeoutException: Message:
任何人都可以幫助我嗎?提前致謝。
uj5u.com熱心網友回復:
您可以在同except一塊或多個except塊中捕獲多個例外
try:
wait = WebDriverWait(driver, 15)
Newest_tab_button = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@text="NEWEST"]//ancestor::*[contains(@resource-id, "itemContainer")]')))
except TimeoutException as e:
print("TimeoutException")
except NoSuchElementException as e1:
print("NoSuchElementException")
except Exception as e3: # To catch an Exception other than the specified once.
print(e3)
或者您需要將所有例外放在一個元組中:
except (TimeoutException,NoSuchElementException): # This catches either TimeoutException or NoSuchElementException
print("ERROR")
uj5u.com熱心網友回復:
在第二種方法中,您只是在捕獲,NoSuchElementException但問題是您的腳本超時并且您收到了TimeoutException,您只需要捕獲它以繼續腳本
from selenium.common.exceptions import NoSuchElementException, TimeoutException
try:
wait = WebDriverWait(driver, 15)
Newest_tab_button = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@text="NEWEST"]//ancestor::*[contains(@resource-id, "itemContainer")]')))
except (NoSuchElementException, TimeoutException) as error:
print(error)
# Continue with the script
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313807.html
上一篇:如何定位特定父div類中的特定div類,pythonselenium
下一篇:用決議器翻頁
