平台上至少还有两个问题,但他们的答案都没有帮助到我。
我刚刚导入了:
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:
有人能帮我吗?提前谢谢。
发布于 2021-10-13 07:10:35
您可以在同一个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")
发布于 2021-10-13 05:13:11
在第二种方法中,您只捕获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://stackoverflow.com/questions/69556141
复制相似问题