如何单击“确定"使用 Python + Selenium 的警报中的按钮 [英] How to Click the "OK" Button within an Alert using Python + Selenium
本文介绍了如何单击“确定"使用 Python + Selenium 的警报中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在弹出的对话框中点击确定"按钮
I want to click the "OK" button in this pop up dialog
我试过了:
driver.switchTo().alert().accept();
但它不起作用
推荐答案
要点击alert 您需要为所需的 alert_is_present() 引入 WebDriverWait
,您可以使用以下解决方案:
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()
注意:您必须添加以下导入:
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
<小时>
参考
您可以在以下位置找到一些相关讨论:
Reference
You can find a couple of relevant discussions in:
- 警报时的 Python 单击按钮
- 如何使用 Python + Selenium 从警报框中读取文本
- 为什么通过 selenium 切换到警报是不稳定?
- 想了解为什么 switch_to_alert() 收到删除线以及如何修复
这篇关于如何单击“确定"使用 Python + Selenium 的警报中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文