Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

enter image description here

I tried:

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

but it doesn't work

like image 607
Candra Herk Avatar asked May 17 '20 21:05

Candra Herk


1 Answers

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.common.alert import Alert
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
like image 127
undetected Selenium Avatar answered Oct 31 '22 10:10

undetected Selenium