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, 2020 in Selenium by karthik
• 120 points
793 views

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.

driver.switchTo().alert().dismiss();

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

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

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

driver.switchTo().alert().getText();

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

driver.switchTo().alert().sendKeys("Text");

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.chrome.ChromeDriver;
import org.openqa.selenium.NoAlertPresentException; 
import org.openqa.selenium.Alert;

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

driver.findElement(By.id("user_full_name")).sendKeys("username"); driver.findElement(By.id("input-lg text user_email_ajax")).sendKeys("username.xyz.net");
driver.findElement(By.id("user_password")).sendKeys("Your_Password");
driver.findElement(By.id("user_submit")).click();

Thread.sleep(5000);

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

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

System.out.println(alertMessage); // Print Alert Message
Thread.sleep(5000);
alert.accept(); 
}
}

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, 2020 by Niroj
• 82,880 points

Related Questions In Selenium

+1 vote
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,500 points
6,087 views
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 6, 2018 in Selenium by nsv999
• 5,500 points
8,708 views
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
9,743 views
+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
10,179 views
0 votes
2 answers

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,500 points
6,240 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
6,596 views
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
4,223 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
5,220 views
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 7, 2020 in Selenium by Suhana

edited Jul 7, 2020 2,967 views
0 votes
2 answers

How to work with blocks in Selenium?

Hello @Beks, You can simply use the jQuery each() method ...READ MORE

answered Nov 25, 2020 in Selenium by Niroj
• 82,880 points
281 views