首页 > 解决方案 > 使用 Python 在 Selenium 中等待 elementID 或简单警报

问题描述

在尝试自动化 Selenium 和 python 中的任务时,我正在构建等待语句以查找元素或警报。我知道我错过了一些东西。

单击元素后,结果将是一个带有特定按钮的新页面(具有已知的“buttonID”),或者将生成一个简单的警报。我想捕捉这些事件中的任何一个并采取行动。

我可以等待按钮或警报:

driver.find_elements(By.ID,"buttonID")

expected_conditions.alert_is_present()

我可以成功地结合使用寻找两个元素

WebDriverWait(driver,10).until(
lambda driver: driver.find_elements(By.ID,"buttonID1) or driver.find_elements(By.ID,"buttonID2"))
)

但是,我在构建结合 find_elements 和 expected_conditions 的等待时遇到问题。基本上我正在尝试:

If buttonID is present:
   Do X
If a simple alert is present:
   Do Y

包装它以尝试捕获任何超时。

标签: pythonseleniumselenium-webdriver

解决方案


推荐阅读