7

I want to read the message which is there in Alert.

Example: If a alert shows "Wrong E-mail address". How to read it? Means i want to store that message in a string.

How to click on OK inside Alert...??

How to do it using Selenium ?

Prasad
  • 472
  • 5
  • 15
Ranadheer Reddy
  • 4,202
  • 12
  • 55
  • 77

2 Answers2

12

I am assuming you are using the Selenium WebDriver.

// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.switchTo().alert();
// Get the text of the alert or prompt
alert.getText();  
// And acknowledge the alert (equivalent to clicking "OK")
alert.accept();

The answer was found here.

If you are using Selenium RC, take a look this webpage.

Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
0

I found that in Selenium RC if you know there's gonna be a confirmation button, then you need to add selenium.getConfirmation(); to your code.

Here's how I've used it (Note: I'm using Java in Eclipse)

selenium.click("//input[@id=\"accepted-emails\"]"); // Confirmation box after this line
if (selenium.isConfirmationPresent())
    String confirmationString = selenium.getConfirmation(); // this line is needed
selenium.keyPressNative("10"); // to press the OK key
selenium.waitForPageToLoad("30000");
System.out.println(confirmationString); // this is not really needed, but used it to simply show the confirmation box message

Hope this helps!