Confirm Alert Box

This type of alert box is used to verify or accept the confirmation from the user. Confirm alert box contains “OK" and "Cancel" buttons.

”OK button” - if a user clicks on an ok button it takes true value.

“Cancel button” - if a user clicks on cancel button it takes false value.


By ID

Example:

  • Launch the webpage "http://www.dummypoint.com/Windows.html"
  • Click on the confirm alert button using name locator.
  • Create the object for Alert class and pass driver object
  • Click on the "Cancel" button using dismiss() method.
  • Wait for 2 sec.
  • Close the Web Browser


Confirm_Alert_Popup.py

from selenium.webdriver.support import expected_conditions as ec
from selenium.common.exceptions import ElementNotVisibleException, ElementNotSelectableException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.alert import Alert
import time

driver = webdriver.Chrome("/Users/admin/Documents/Skill2Lead/others/drivers/chromedriver.exe")  # Using Chrome Driver

driver.get("http://www.dummypoint.com/Windows.html")
assert "Selenium Template" in driver.title

wait = WebDriverWait(driver, 25, poll_frequency=1,
                     ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException,
                                         NoSuchElementException])

wait.until(ec.presence_of_element_located((By.NAME, 'confirmalertb'))).click()
time.sleep(2)

# Import Alert class
# Create the object for Alert class and pass driver object
a_button = Alert(driver)
time.sleep(2)


# accept() method clicks on "OK" button.
#a_button.accept()

# dismiss() method clicks on "Cancel" button.
 a_button.dismiss()

time.sleep(2)
driver.quit()