Handling Javascript Popups Using Selenium-WebDriver

We can use the below methods to handle Javascript popups.

# accept()
This method can be used to click 'OK' button in a popup
  1. WebDriver firefoxDriver=new FirefoxDriver();  
  2. Alert alert = firefoxDriver.switchTo().alert();//Creating object for Alert class  
  3. alert.accept();//Clicking OK button  

# dismiss()
Canceling popup using dismiss method
  1. WebDriver firefoxDriver=new FirefoxDriver();  
  2. Alert alert = firefoxDriver.switchTo().alert();  
  3. alert.dismiss();//Canceling the popup  

# getText()
Getting popup text
  1. String strAlertText;  
  2. WebDriver firefoxDriver=new FirefoxDriver();  
  3. Alert alert = firefoxDriver.switchTo().alert();  
  4. strAlertText=alert.getText();//This line retrieves text from popup.   
  5. System.out.println(strAlertText);