如何使用Python + Selenium在警报中单击“确定”按钮

我想在此弹出对话框中单击“确定”按钮

enter image description here

我试过了:

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

但这不起作用

评论
kquam
kquam

To click on the OK button within the alert you need to induce WebDriverWait for the desired alert_is_present() and you can use the following solution:

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

参考

您可以在以下位置找到一些相关的讨论:

点赞
评论