Alerts and popups are one of the important features in any modern-day web
application and handling it via Selenium sometimes could be tricky.
But before moving further let's understand about
different types of popups in any web application.
In SELENIUM, pop up is
categorized into the following types.
1. Javascript Popup
2. Hidden Division Popup
3. File Upload popup
4. File download popup
5. Child browser popup
6. Window popup
In this article, we will be looking more into javascript popup which further has been broadly divided into Alert, confirmation and prompt popup.
In this article, we will be looking more into javascript popup which further has been broadly divided into Alert, confirmation and prompt popup.
Javascript Pop up:
This pop up is
subdivided into below mentioned 3 pop-ups.
1. Alert pop up
2. Confirmation pop up
1.
Alert Pop up:
The main Characteristics
features of this pop-up are:
● We can’t inspect this
pop-up and move this kind of popup.
● This pop up will have a white color background with the black
color font.
● This pop up will have
only one “OK” button
Steps
to handle Alert pop up
To handle the alert pop
up, we first have to switch to an alert window using the below statement.
driver.switchTo().alert();
After transferring the
control to the alert window, we can use the following methods of the “Alert”
interface.
getText() → to get the text present
on the alert window.
accept() / dismiss() → to click on OK button
on the alert window.
Sample code to handle Alert Popup
Characteristics features
of this pop-up are :
● We can’t inspect this
pop-up.
● We can’t move this
kind pop up.
● This pop up will have
a white color background with the black color font.
● This pop up will have
two buttons:- “OK” button and the “Cancel” button.
Steps
to handle Confirmation pop up
To handle the alert pop
up, we first have to switch to an alert window using the below statement.
driver.switchTo().alert();
After transferring the
control to the alert window, we can use the following methods of the “Alert”
interface.
Alert.accept()--To
Accept the alert.
Alert.dismiss()--To
Dismiss the alert
Sample code to handle confirmation Popup
A prompt box is often
used if we want the user to input a value before entering a page.
When a prompt box pops
up, the user will have to click either "OK" or "Cancel" to
proceed after entering an input value.
If the user clicks
"OK," the box returns the input value. If the user clicks
"Cancel" the box returns null.
Steps
to handle Prompt Alert pop up
● To handle the alert
pop up, we first have to switch to an alert window using the below statement.
driver.switchTo().alert();
● After transferring the
control to the alert window, we can use the following methods of the “Alert”
interface.
getText() → to get the text present
on the alert window.
sendKeys() → to enter a text in the
textbox on the alert window
accept() → to click on the “OK” button on the alert window.
dismiss() → to click on the “Cancel” button on the alert window.
Sample
code to Handle Prompt Alert Popup
Different types of Alert popup exceptions that
we usually face
1. NoAlertPresentException: When the alert pops up
is physically not available on the page, and we still try to either switch to
the alert or try to perform any action on the alert popup, we get this
exception.
2. UnhandledAlertException: We get this exception when we perform any action
on the webpage without handling the alert popup.
To know more about different types of exceptions and how to handle them in selenium. Refer to this article.
To know more about different types of exceptions and how to handle them in selenium. Refer to this article.