package com.exceptionsSelenium; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class AlertHandle { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://qa.scrolltest.com"); try { driver.findElement(By.name("qaid")).sendKeys("#123"); driver.findElement(By.name("submit")).click(); String alerttext = driver.switchTo().alert().getText(); System.out.println("alerttext - "+alerttext); driver.switchTo().alert().accept(); }catch(NoSuchElementException e) { System.out.println(e.toString()); } driver.quit(); } }