Selenium Interview : How do you handle alert pop-up in Selenium?

How do you handle alert pop-up in Selenium ?

Answer: In Selenium webDriver, to handle alert pop-ups, we first need to switch control to alert pop-ups then click on ok or cancel button available on alert then move control back to main page.

The sample code is follows:

String mainPage = driver.getWindowHandle();
 
Alert myAlert= driver.switchTo().alert(); // to move control to alert popup
 
myAlert.accept(); // to click on ok.
 
myAlert.dismiss(); // to click on cancel.
 
//Then move the control back to main web page-
 
driver.switchTo().window(mainPage);

 

 

Be First to Comment

Leave a Reply

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