SEMrush

Selenium Webdriver - Pop ups and Alerts in Java


An alert dialog box is special dialog box that is displayed in a graphical user interface when something unexpected occurred that requires immediate user action.

The typical alert dialog provides information in a separate box to the user, after which the user can only respond in one way i.e by closing it. 
Original window, which is not available while the alert dialog is presented, can be accessed after closing the alert.

Defining an Alert or a Pop up


Alert is a small box that appears on the display screen . It can shows some information or warning or can ask for permissions .

Example: Let us consider a real life example to understand this concept.  Suppose we are navigating through a social networking website and accidentally clicked on delete button which you never meant to delete. So system will warn  us against our delete action, prompting – 
"Do you really want to delete the file?" with Yes and No options to respond.Thus giving you a chance to save the file you actually not meant to delete.
Have a look at the pop ups and alerts below 

webdriver alerts 1


Alerts are used in the following cases :
  • Info: presents a general notification about a recent event.
  • Error: error alerts inform the user that an operation could not complete due to some insurmountable error.
  • Warning: alerts that the current course of action could be in some way dangerous or detrimental, . It thus offers the user options to proceed or stop the execution.
  • Question: Elicits some kind of response from the user, required to complete the current process.
Warning and question alerts offer two opposite options to close the dialog ("Allow/Deny", "OK/Cancel", "Yes/No") with the implicit assumption that one will proceed with the paused process that triggered the dialog, and the other one will interrupt the process without action. 


Code to Accept an Alert

Alert alert = driver.switchTo().alert();
alert.accept(); 


Previous                                                                          Next