Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

How to handle alert in Selenium Webdriver

Category: Selenium WebDriver

Web-Based alert and Java Script alerts are same so do not get confused.
Alert Interface has some methods-
1- accept()- Will click on the ok button when an alert comes.
2- dismiss()- Will click on cancel button when an alert comes.

//Captured Alert Text (Actual Text)
String actualAlertMessage = driver.switchTo().alert().getText();

//Accept the alert (Click OK)
driver.switchTo().alert().accept();

//Accept the alert (Click Cancel)
driver.switchTo().alert().dismiss();

//Send “Hire QA” to Alert’s text box
driver.switchTo().alert().sendKeys(“Hire QA”);

Leave a Reply

Your email address will not be published. Required fields are marked *