In this article we learn how to handle alert in selenium, types of alert and also we learn most commonly used methods in alert popup.
Alert in Selenium
Alert Popup: –
It is a simple popup which shows some information to user in a small box within browser area.
It will have one “OK” button and close button.
Popup will be disappeared when user clicks on either OK button or close button.
We cannot inspect alert popup. We cannot handle it with selenium webdriver directly. So we need to use Alert interface.
Alert is an interface in selenium library.
Alert Popup methods:
1) accept () : To click on OK button on popup.
2) dismiss (): To click on close/cancel button of popup.
3) getText (): To get text present on popup.
4) sendKeys (java.lang.String keysToSend): To type on prompt popup
How to Handle Alert in Selenium?
To handle alert popup, first we need to switch to alert using switchTo () method
Syntax:
Alert alt= driver. switchTo().alert();
alert () switches to the currently active popup for particular driver instance and return an Alert reference. It will throw NoAlertPresentException if no alert is present
Types of Alert Popup: –
Confirm Popup: –
This popup is used to ask confirmation from user to perform some action within browser.
This popup will have two buttons “OK” and “Cancel”. Both buttons will lead to different actions.
Like alert popup, we cannot inspect it also.
We need to use Alert interface to handle it in selenium webdriver.
Prompt Popup: –
This popup is used to get input from user as a text.
This popup will have a text box with OK and Cancel button.
Like alert popup, we cannot inspect it also.
We need to use Alert interface to handle it in selenium webdriver.
Alert interface provides a method to type text in text box.
Prompt popup will have single text box only.