How to Click the “OK” Button within an Alert using Python + Selenium

心不动则不痛 提交于 2021-01-27 12:52:53

问题


I want to click the "OK" button in this pop up dialog

I tried:

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

but it doesn't work


回答1:


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 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


来源:https://stackoverflow.com/questions/61859356/how-to-click-the-ok-button-within-an-alert-using-python-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!