How to Handle alerts like Irctc alert in selenium?

0 votes
Hi I am using alert.acept but still could not able to get it answer(IRCTC home page alert)
Jun 27 in Selenium by karthik
• 120 points

1 answer to this question.

0 votes

Hello  karthik,

Handling alerts manually is a tedious task. To reduce human intervention and ease this task, Selenium provides a wide range of functionalities and methods to handle alerts.

The following methods are useful to handle alerts in selenium:

1. Void dismiss(): This method is used when ‘Cancel’ button is clicked in the alert box.


2. Void accept(): This method is used to click on the ‘OK’ button of the alert.


3. String getText(): This method is used to capture the alert message.


4. Void sendKeys(String stringToSend): This method is used to send data to the alert box.


Here how exactly alerts in selenium works with the help of an example.

Let’s write a Selenium test script to handle alerts.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.NoAlertPresentException; 
import org.openqa.selenium.Alert;

public class AlertHandlingDemo {
public static void main(String[] args) throws NoAlertPresentException,InterruptedException { 
System.setProperty("","Path_of_Chrome_Driver"); //mention dummy path or variable that stores chrome driver path 
WebDriver driver = new ChromeDriver(); driver.get("");

driver.findElement("user_full_name")).sendKeys("username"); driver.findElement("input-lg text user_email_ajax")).sendKeys("");


Alert alert = driver.switchTo().alert(); // switch to alert

String alertMessage= driver.switchTo().alert().getText(); // capture alert message

System.out.println(alertMessage); // Print Alert Message

When you execute the above code, it navigates through the Sign up page, inputs the necessary details, hits the Sign me up button and throws the alert.

Next, the driver will switch to alert, try to capture the alert message, and then display the message.

Wish to know about Selenium Automation Testing with Java, have a glance at the detailed Selenium Tutorial. 

Thank You!!

answered Jun 29 by Niroj
• 41,480 points

Related Questions In Selenium

0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,510 points
0 votes
1 answer

How to handle Pop-up in Selenium WebDriver using Java

Actually, its pretty simple. Use this code ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points
0 votes
1 answer

How to Check if any alert exists in selenium using Python

This might work for you. It did ...READ MORE

answered May 10, 2018 in Selenium by king_kenny
• 3,710 points
+1 vote
1 answer

How to handle notifications in Python with Selenium (Chrome WebDriver)

Below will help you: You can disable the ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,890 points
0 votes
3 answers

How to stop a page loading from selenium in chrome?

Hi Savan, Setting the pageLoadStrategy capability to none. Then wait for ...READ MORE

answered Jul 6 in Selenium by Suhana

edited Jul 6 152 views
+2 votes
1 answer

How to fetch the text of a web element where the text is split in 2 lines?

Hello, You want to use a non-breaking space ...READ MORE

answered May 28 in Selenium by Niroj
• 41,480 points