Prompt Alert Box

This type of alert box is used for users to enter the values or some text in the edit box and verify that value when we click on the ok button. It contains an “OK and Cancel button”.

“ok button” - It takes the entered data when we click on the ok button.

“cancel button” - If a user clicks on “cancel button” it doesn’t take any data.


By ID

Example:

  • Launch the webpage "http://www.dummypoint.com/Windows.html"
  • Click on the Promt alert button using name locator.
  • Create the object for Alert class and pass driver object
  • Get the text which is on alert popup.
  • Enter the text in prompt Alert using send_text() method.
  • Click on the "OK" button using accept() method.
  • Wait for 2 sec.
  • Close the Web Browser


Prompt_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/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, 'promtalertb'))).click()
time.sleep(2)

# Import Alert class
# Create the object for Alert class and pass driver object
a_button = Alert(driver)

time.sleep(2)

# Get the text which is on alert popup
text_p = a_button.text
print(text_p)

# Enter the text in prompt Alert
a_button.send_keys("Skill2Lead")

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

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

time.sleep(5)
driver.quit()