问题
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 ?
回答1:
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.
回答2:
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!
来源:https://stackoverflow.com/questions/10633889/read-message-from-alert-and-click-on-ok