Tuesday, November 4, 2014


A JavaScript confirm popup box is used when you want the user to verify or accept something. In confirm box, the user can click either "OK" button or "Cancel" button to proceed.

If the user clicks on "OK" button, the confirm popup box will return true. If the user clicks on "Cancel" button, the confirm popup box will return false.





JavaScript Confirm Box Example


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ConfirmExample {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to(
    "http:// www.w3schools. com/js/tryit.asp?filename=tryjs_confirm");

  driver.switchTo().frame(0);

  Thread.sleep(5000);
  WebElement element = driver.findElement(By.tagName("button"));
  element.click();

  Alert alert = driver.switchTo().alert();

  System.out.println(alert.getText());
  Thread.sleep(5000);
  alert.accept();

 }

}