Selenium WebDriver -Handling JavaScript alerts
Handling JavaScript alerts of a web page using Selenium Webdriver is illustrated with an example below. On clicking on link/button in a webpage,we can shift the focus to java script alert/prompt by using driver.switchTo().alert() . import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class javascriptalerts { @Test public void auth() { WebDriver driver=new FirefoxDriver(); driver.get("http://the-internet.herokuapp.com/javascript_alerts"); //1.click on javascript alert driver.findElement(By.xpath("html/body/div[2]/div/div/ul/li[1]/button")).click(); System.out.println(driver.switchTo().alert().getText());//To fetch the text in the alert driver.switchTo().alert().accept();//clicking on OK button can be done using accept method.*/ //2.click on javascript co...