This type of alert is used to show some sort of text to a user along with a “ok button”.
“Ok button” - User should click on the ok button to proceed to the next step.
1. Import Alert class
from selenium.webdriver.common.alert import Alert
2. Create the object for Alert class and pass driver object.
driver = webdriver.Chrome("/Users/admin/Documents/Others/Drivers/chromedriver.exe") # Using Chrome Driver
a_button = Alert(driver)
3. Now using alert object call the methods to perform actions.
a_button.accept()
Example:
AlertPopup.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, 'alertbutton'))).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)
# accept() method clicks on the "OK" button.
a_button.accept()
time.sleep(5)
driver.quit()