How to Handle Popup and Alerts in Selenium Webdriver using Java ?

.
             Handling Alerts and Popups in Selenium 

Handling Alerts :

  • What is Alert in Selenium Webdriver ?
Alert in selenium webdriver is small message boxes that notify the user of some notification or message or ask some permission to perform certain kinds of task. it is also used for warning purposes or to enter some details in the alert box as well.

Types of Alert in Selenium Webdriver 
Alerts in selenium are categorised into three-part.
  • Simple Alert 
  • Confirmation Alert 
  • Prompt Alert 
  • How to Click on the "Cancel" Button of the Alert?

dismiss() - In selenium webdriver to click on the cancel button of alert we use dismiss method .dismiss method is a predefined method of selenium alert class.

Selenium Code - 

// How to click on cancel button
Alert alt = driver.switchTo().alert();
alt.dismiss();
System.out.println(" cancel button clicked successfully");

  • How to Click on the "Ok" Button of the alert?

accept()- In selenium to click on the ok button of the alert we use accept method. 

Selenium code

//How to Click on ok button
Alert alt= driver.switchTo().alert();
    alt.accept();
System.out.println(" OK button clicked successfully");

  • How to get or capture an alert message?

getText() - In selenium to get the message of alert we use the get text method.

Selenium Code 

//How to get text from alert window 
String altmsg=driver.switchTo().alert().getText();
 System.out.println(altmsg);

//To verify the message 

if(altmsg.contains("Do you really want to delete this Customer?")) 
{
System.out.println("valid Message ");
}
else {
System.out.println("invalid Message");
}

  • How to Enter the Data in the Text Box on alert?
sendKeys()- sendkeys method in selenium is used to send data to alert box.

Selenium Code -
 //How to handle promt alert
Alert alt = driver.switchTo().alert();
alt.sendKeys("abc");
alt.accept();



Hope !!!Above tutorial on handling alerts and popups in selenium is helpful for you ...
Team 
QA acharya

Tags: Alert in selenium , how to handle alert in selenium , how to automate alert in selenium , how to capture text in selenium .
Fig. How to handle alert in selenium 


.

Post a Comment

0 Comments