An alert on the webpage used to get the attention of the user to perform some operation on the alert or the webpage; sometimes alerts expect input from the user.
Alerts take the focus away from the current window and force the browser to read the message.
Alert prevents the user from accessing other parts of the web page until the box is closed.
Alert Types :
Properties of Alerts with respect to Selenium Python
When an alert is present on the webpage, we cannot proceed further without handling the popup, and if we try to perform any operation, it throws selenium.common.exceptions.UnexpectedAlertPresentException.
Syntax to handle Alert
ale = driver.switch_to_alert();
With this alerts API, we can perform below operations on the pop-up.
1. Accept the popUp by clicking OK button
2. Dismiss the popUp by clicking the 'X' icon.
3. Get popUp text by getText method
4. Send a text to popUp by send_keys (Applicable for Prompt only)
When do alerts occur on a page : It can occur on a page on any time, but most of it happens on below timing of the webpage
The alert() method displays an alert box with a message and an OK button, an alert box is often used to inform a user about a particular operation like details saved in Database, right-click disabled, Loaded successfully such kind of messages.
Alert's sole purpose is to inform the user, nothing more.
Alert on Different browsers
driver.switchTo().alert(); we, wee this object in Alert type variable 'ale.'
def test_open_alerts(self): driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe') driver.get("https://chercher.tech/java/practice-pop-ups-selenium-webdriver"); driver.find_element_by_name("alert").click(); ale = driver.switch_to_alert() # clicks 'OK' button ale.accept();
5. We can dismiss the popup using the dismiss() method from alert API, this click 'X' icon on the popup. (if you have performed step 4, you cannot perform this step as pop up is closed in step 4 itself)
ale = driver.switch_to_alert(); # clicks 'x' icon ale.dismiss();
6. We can get the text from the pop up using text; if you have performed step 4 or 5, this step will not work
ale = driver.switch_to_alert(); # clicks 'x' icon ale.text;
A confirmation box is the second kind of alert; it displays a dialog with OK and Cancel button.
Confirmation box informs the developer about user choice whether the user pressed OK or Cancel. The confirm() method returns true if the user clicked "OK", and false otherwise('X' icon and 'Cancel') to the developer.
We can handle Confirmation box in selenium python like an alert box, there is no coding difference.
Prompt is used to get value from the user in text format. Prompt provides text bar for user input; Prompt is rarely used alert type.
Prompt also follows the same coding as alert and prompt except the sendkeys method, and we can send a text to prompt text bar using the sendkeys() method in alerts API.
driver.get("https://chercher.tech/java/practice-pop-ups-selenium-webdriver"); driver.find_element_by_name("prompt").click(); ale = driver.switch_to_alert(); ale.send_keys("chercher.tech");