How to handle Alerts in Selenium WebDriver?

In this tutorial, we will discuss alert that pop-up while working with web applications and how to handle them. The very first question that arises is “What is an Alert?”

  • Alert is a small box that appears on the display screen with a message and an OK button.
  • The message can be some information or a warning.
  • Alerts couldn’t be ignored. It needs to be accepted or rejected before you can proceed ahead with your further actions on the web page. For example- You click on any button on a web page it gives an alert with some message. Now you can either accept that alert or reject it.

SCENARIO-1 

Step 1- Navigate to URL http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert

Step 2- Click “Try it” button.

Step 3- An alert will pop-up with a message “I am an alert box”. Get this message through the script.

Step 4- Click “OK” to accept the message.

Please refer below image for more clarity.

Accepting the Alert

So, here is the code for above steps:

public class AlertHandling {

	public static void main(String[] args) throws InterruptedException {

		FirefoxDriver Driver = new FirefoxDriver(); // launching browser

		String url = "http://www.w3schools.com/jsref/tryit.asp? //URL to be invoked

		filename=tryjsref_win_open"; Driver.manage().window().maximize(); //Maximising browser window

		Driver.manage().deleteAllCookies(); //Deleting All cookies

		Driver.get(url); //Invoking URL

		Driver.switchTo().frame("iframeResult"); //Switching to frame on same web page

		Driver.findElement(By.tagName("button")).click(); // Locating and clicking on "Try it" button

		Alert alert = Driver.switchTo().alert(); //Switching to Alert on same web page

		String messageOnAlert = alert.getText(); // Fetching message on Alert box in a String

		System.out.println("Message on Alert : "+ messageOnAlert); //Displaying the message on Alert message

		Thread.sleep(5000); // Explicit wait of 5 seconds so that you can see Alert for sometime

		alert.accept(); //Accepting an alert



		Driver.switchTo().defaultContent(); //Switching to parent page

		Driver.quit(); //Quitting browser

	}

}

EXPLANATION- Few lines of code remains same which we already discussed in earlier posts. Here the main objective was handling the alert. This web page comprised of frames. So, we switched to the frame (Frame would be covered in detail in later topics), the reason behind switching to the frame was to click on “Try it” button as it was present on the frame on the same web page. Clicking on “Try it” button, pop-up an alert. Now, our objective is to switch to alert, fetch the message from Alert box and later clicking on “OK” to accept alert. So, these lines of code would take care of it.

Alert alert = Driver.switchTo().alert(); // switchTo().alert() method switches to Alert on same web page

String messageOnAlert = alert.getText(); // getText() method Fetches message on Alert box in a String

System.out.println(“Message on Alert : “+ messageOnAlert); //Displaying the message on Alert message

Thread.sleep(5000); // Explicit wait of 5 seconds so that you can see Alert for sometimes

alert.accept(); //accept() method accepts an alert

SCENARIO 2:

Step 1- Click on this URL http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm

Step 2- Click on “Try it” button.

Step 3- An alert will pop-up with a message “Press a button”. Get this message through the script.

Step 4- Click on “Cancel” to dismiss the message.

Dismissing the Alert

Dismissing the Alert

EXPLANATION- Entire code would remain same that we used for accepting the alert. Here, instead of accepting alert we are dismissing or canceling it so, the code would be-

Alert alert = Driver.switchTo().alert(); // switchTo().alert() method switches to Alert on the same web page

String messageOnAlert = alert.getText(); // getText() method Fetches message on Alert box in a String

System.out.println(“Message on Alert : “+ messageOnAlert); //Displaying the message on Alert message

Thread.sleep(5000); // Explicit wait of 5 seconds so that you can see Alert for sometimes

alert.dismiss(); //dismiss() method dismisses an alert

So, this was all about handling an alert on any web page. Just to brief, an alert is a pop-up on any web page which can’t be ignored. It requires user action. Actions could be of two types, either accept it or dismiss it.

For any questions, queries or feedback feel free to write us at amrita@qatechhub.con or support@qatechhub.com. Happy learning! 🙂

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: