How would you handle the alert popups in Selenium Webdriver?

915    Asked by JordanDuncan in QA Testing , Asked on Feb 15, 2020
Answered by Jordan Duncan

 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.

Your Answer


Parent Categories