We can use the below methods to handle Javascript popups.
# accept()
This method can be used to click 'OK' button in a popup
# dismiss()
Canceling popup using dismiss method
# getText()
Getting popup text
# accept()
This method can be used to click 'OK' button in a popup
- WebDriver firefoxDriver=new FirefoxDriver();
- Alert alert = firefoxDriver.switchTo().alert();//Creating object for Alert class
- alert.accept();//Clicking OK button
# dismiss()
Canceling popup using dismiss method
- WebDriver firefoxDriver=new FirefoxDriver();
- Alert alert = firefoxDriver.switchTo().alert();
- alert.dismiss();//Canceling the popup
# getText()
Getting popup text
- String strAlertText;
- WebDriver firefoxDriver=new FirefoxDriver();
- Alert alert = firefoxDriver.switchTo().alert();
- strAlertText=alert.getText();//This line retrieves text from popup.
- System.out.println(strAlertText);