Read Message From Alert and click on OK

Ranadheer Reddy picture Ranadheer Reddy · May 17, 2012 · Viewed 20.2k times · Source

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 ?

Answer

Ranhiru Jude Cooray picture Ranhiru Jude Cooray · May 17, 2012

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.