Hello (excuse my English),
I have the following problem using Selenium WebDriver with IE 11. When I try catching alert text, I manage getting the first text on the first alert. As soon as I accept the first alert, the second alert is displayed but the text is empty although I can accept it. I really don't understand this problem. Can you help me please ?
Here is my code :
public class AlertDemo {
static WebDriver driver;
static Wait<WebDriver> wait;
static String driverPath =
"C:\\Users\\Acer\\Downloads\\IEDriverServer_Win32_2.53.1\\";
public static void main(String[] args) throws
NoAlertPresentException,
InterruptedException {
System.out.println("*******************");
System.out.println("Lancement INTERNET EXPLORER pour
tester la popup");
System.setProperty("webdriver.ie.driver", driverPath +
"IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
// Alert Message handling
driver.get("http://demo.guru99.com/test/delete_customer.php");
driver.findElement(By.name("cusid")).sendKeys("53920");
driver.findElement(By.name("submit")).submit();
// Switching to Alert
try {
WebDriverWait wait = new WebDriverWait(driver,
2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
// Capturing alert message.
String alertMessage =
driver.switchTo().alert().getText();
// Displaying alert message
System.out.println(alertMessage);
// Accepting alert
System.out.println("On accepte OK");
alert.accept();
Thread.sleep(500);
//Pour le message en retour
/*wait = new WebDriverWait(driver, 3);
wait.until(ExpectedConditions.alertIsPresent());*/
Alert alert2 = driver.switchTo().alert();
System.out.println("TEXTE " +
alert2.getText());
System.out.println("TEXTE2 " +
alert.getText());
Thread.sleep(5000);
if (alert2 == null) {
System.err.println("Pas de popup !!");
} else {
System.out.println("OK la popup pour le
retour n'est pas nulle");
Thread.sleep(2000);
if
(!"".equals(driver.switchTo().alert().getText())) {
System.out.println(" Le texte
de la popup suivante est " + driver.switchTo().alert().getText());
alert2.accept();
} else {
System.err.println("Erreur: on
arrive pas à retrouver le texte");
System.out.println("Je vais
cliquer OK");
alert2.accept();
}
}
} catch (NoAlertPresentException e) {
e.printStackTrace();
System.err.println("Pas trouvé d'alert");
}
}
}
The aim of this is to get String text contained in the second alert.
When you accept the first alert, second alert comes with the checkbox that says "Don't let this page create more messages". In Internet Explorer driver somehow it prevents it to be read. Other browsers such as Chrome, there is not any problem.
To solve this issue, you simple need to add the website into trusted sites
in Internet Explorer options. Then, you can get the text of the second alert.