Read Message From Alert and click on OK

∥☆過路亽.° 提交于 2020-01-23 05:46:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!