python - 如何在selenium(python,chromedriver)中接受由 'get'触发的警报?

标签 python selenium selenium-chromedriver

我正在尝试使用 selenium 从某个页面导航到另一个页面:

driver = webdriver.Chrome()
driver.get("...some page...")
...  # the alert does not exist yet and thus cannot be accepted
driver.get("...some other page...") # the alert pops up here and blocks navigation to 'some other page'
# execution never reaches here
...

现在,离开“某个页面”会触发警报,要求确认用户确实想要离开该页面。这会永远阻止执行。设置了隐式超时,但不会由此触发。我无法让 selenium 接受警报,因为仅在调用“get”后出现。

有什么办法可以解决这个问题吗?

非常感谢!

最佳答案

尝试以下代码,如果不起作用请告诉我

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome()
driver.get("...some page...")
try:
    WebDriverWait(driver, 5).until(EC.alert_is_present())
    driver.switch_to_alert().accept()
except TimeoutException:
    print("Alert not found. Move on...)
driver.get("...some other page...")

关于python - 如何在selenium(python,chromedriver)中接受由 'get'触发的警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40259660/

相关文章:

python - 在 python 中更改字典

javascript - 单击按钮时出现 Selenium 错误 : . MoveTargetOutOfBoundsException

selenium - 如果单元格的 xpath 不匹配,如何忽略从单元格获取表格文本

google-chrome - Webdriver 在 Linux 测试中随机生成 'chrome not reachable'

excel - 使用 FindElementbyXpath() 获取 Selenium Basic 中可填充框的行和列名称

python - 在管道中使用 apache beam 参数

python - 类变量是可变的吗?

python - 机器人框架: Using faker to generate a prefix string for account data

python - 最新版本的 firefox 不适用于 selenium

java - Selenium:setHeadless() 对 ChromeDriver 没有影响