Handling Alerts with Selenium

 

// How to handle alerts in Selenium

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 HandlingAlerts {

	public static void main(String[] args) throws InterruptedException {
		WebDriver driver = new FirefoxDriver();
		String Url = "http://sislands.com/coin70/week1/dialogbox.htm";
		driver.get(Url);
		WebElement alert = driver.findElement(By.xpath("html/body/div[1]/center/table/tbody/tr/td/form[1]/p/input"));
		alert.click();
		Alert popup = driver.switchTo().alert();
		System.out.println(popup.getText());
popup.accept();
		Thread.sleep(3000);
		
		driver.close();

		
		
	}
}