7

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Candra Herk
  • 441
  • 3
  • 6
  • 13

1 Answers1

15

To click on the OK button within the 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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • NameError: name 'WebDriverWait' is not defined – Candra Herk May 17 '20 at 22:27
  • TypeError: __init__() takes 1 positional argument but 2 were given – Candra Herk May 17 '20 at 22:34
  • @CandraHerk Checkout the updated answer and let me know the status. – undetected Selenium May 17 '20 at 22:37
  • DevTools listening on ws://127.0.0.1:62996/devtools/browser/dc61f8e2-7ff6-4713-8e10-7d5d71eec38a [34368:48752:0518/053839.730:ERROR:browser_switcher_service.cc(238)] XXX Init() Traceback (most recent call last): File "C:\Users\herk\Programming\intro.py", line 19, in WebDriverWait(driver, 10).until(EC.alert_is_present).accept() File "C:\Users\herk\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until value = method(self._driver) TypeError: __init__() takes 1 positional argument but 2 were given – Candra Herk May 17 '20 at 22:42
  • driver.switch_to().alert().accept() TypeError: 'SwitchTo' object is not callable – Candra Herk May 17 '20 at 22:51
  • @CandraHerk Checkout the updated answer and let me know the status. – undetected Selenium May 17 '20 at 22:56
  • Maybe a bit late but it works when I did `self.switch_to.alert.accept()`. remove the parenthesis to the `switch_to` and `alert`. – KiritoLyn Dec 24 '21 at 11:49