Ranadheer Reddy
Ranadheer Reddy

Reputation: 4324

Read Message From Alert and click on OK

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 ?

Upvotes: 7

Views: 20922

Answers (2)

Mayur Padgaonkar
Mayur Padgaonkar

Reputation: 1

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!

Upvotes: 0

Jude Cooray
Jude Cooray

Reputation: 19862

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.

Upvotes: 12

Related Questions