Alert in Selenium: An alert in
Selenium is a small message box that appears on the screen to give the user
some information or notification.
With the assistance of Selenium, we can execute various tasks within an alert box such as accepting or dismissing it, obtaining alert messages, and sending actions. These actions rely on the use of the class selenium.webdriver.common.alert.Alert(driver).
Selenium alert methods:
- accept() - This method accepts an alert pop-up.
- Syntax:
- alert = Alert(driver)
- alert.accept()
- dismiss() - This method dismisses an alert pop-up.
- Syntax:
- alert = Alert(driver)
- alert.dismiss()
- send_keys() - Send keys to alert
- Syntax:
- alert = Alert(driver)
- alert.send_keys("Testing Colleges")
- text - This method gets the alert message.
- Syntax:
- alert = Alert(driver)
- message = alert.text
- print(message)
Program:
- from selenium import webdriver
- from selenium.webdriver.common.alert import Alert
- from selenium.webdriver.common.by import By
- driver = webdriver.Chrome()
- driver.get("https://testingcolleges.blogspot.com/2023/08/alerts-demo-pages.html")
- # Confirm Alert box
- altbox = driver.find_element(By.ID, "confirmbox")
- altbox.click()
- time.sleep(2)
- alert = Alert(driver)
- # print the alert message
- print(alert.text)
- # accept the alert box
- time.sleep(2)
- # close the window
- driver.close()
OUTPUT:
No comments:
Post a Comment