Read Message From Alert and click on OK

selenium, selenium-rc

Solution

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.

Problem

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 ?

Original source