How to Handle Alert and popup in Selenium Webdriver using Java ?

             Handling alerts and popup in selenium 

  • How to Click on "cancel" button of alert ?

dismiss() - In selenium webdriver to click on cancel button of alert we use dismiss method .dismiss method is a predefine 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 "Ok" button of alert ?

accept()- In selenium to click on ok button of 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 alert message ?

getText() - In selenium to get the message of alert we use 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 text box on alert ?
sendKeys()- sendkey 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();


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

Types of alert in selenium webdriver 
Alert in selenium are categories into three part .
  • Simple Alert 
  • Confirmation Alert 
  • Prompt Alert 

Hope !!!Above tutorial of handling alert and popup in selenium 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