python - 如何使用 Python + Selenium 在警报中单击 "OK"按钮

标签 python selenium selenium-webdriver selenium-chromedriver alert

我想在这个弹出对话框中点击“确定”按钮

enter image description here

我试过:

driver.switchTo().alert().accept(); 

但它不起作用

最佳答案

单击 中的确定按钮您需要为所需的 alert_is_present() 引入 WebDriverWait您可以使用以下解决方案:

WebDriverWait(driver, 10).until(EC.alert_is_present())
driver.switch_to.alert.accept()

备注 :您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

引用

您可以在以下位置找到一些相关讨论:
  • Python click button on alert
  • How to read the text from the alert box using Python + Selenium
  • Why switching to alert through selenium is not stable?
  • Would like to understand why switch_to_alert() is receiving a strikethrough and how to fix
  • 关于python - 如何使用 Python + Selenium 在警报中单击 "OK"按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61859356/

    相关文章:

    java - 使用 Selenium webdriver 从网页中提取表数据

    java - Selenium WebDriver 无法定位 Selenium IDE 可以定位的 Element

    python - 使用 @property 装饰器时,Spyder 不显示文档字符串

    python - PyQt:事件未触发,我的代码有什么问题?

    python - "or"条件导致 "if"出现问题

    selenium - 在 selenium 和 chromedriver 上登录 etrade 时出现问题

    Python 无法从某些 scipy.signal 方法导入名称

    java - 错误 :cannot get automation extension for chrome with selenium

    javascript - JavaScript 可以与 Selenium 2 对话吗?

    java - Selenium Connection Reset : Unable to find an exact match for CDP version 96, 所以返回找到的最接近的版本:95