How would you handle the alert popups in Selenium Webdriver?
First, you’ve to switch the control to the pop up then press the ok or cancel button. After that, turn back to the source page screen.
Code Example:
String srcPage = driver.getWindowHandle();
Alert pop = driver.switchTo().alert(); // shift control to the alert popup.
pop.accept(); // click on the ok button.
pop.dismiss(); // click on cancel button.
// Move the control back to the source page.
driver.switchTo().window(srcPage); // move back to the source page.