Handling Alerts or Popups using Selenium webdriver

package selenium_examples;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Sample {
   public static void main(String[] args)
  
                        {
   
  WebDriver driver = new FirefoxDriver();
                driver.get("http://www.rediff.com");
                driver.findElement(By.linkText("Sign in")).click();
                driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
                driver.findElement(By.id("btn_login")).click();
                Alert alt=driver.switchTo().alert();
               System.out.println(alt.getText());
               alt.accept();
              
     
    }
  }

Output:-
Please enter your email ID

Handling alerts or popups in webdriver


Alert alt=driver.switchTo().alert();->Get a handle to the open alert, prompt or confirmation
alt.getText()->Gets the text of alert
alt.accept()->To Accept any alert, i.e Clicking OK button
alt.dismiss()->To dismiss any alert, i.e Clicking Cancel button



  

No comments:

Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge