2012-05-17 17 views
4

我想閱讀Alert中的消息。從Alert中讀取消息,然後單擊OK

示例:如果警報顯示「錯誤的電子郵件地址」。如何閱讀?意思是我想把這個消息存儲在一個字符串中。

如何在Alert中點擊OK ...?

如何使用硒做到這一點?

回答

7

我假設您使用的是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(); 

答案被發現here

如果您使用的是Selenium RC,請瀏覽this網頁。

+0

我正在使用硒rc。 –

+0

我更新了我的答案,包括Selenium RC。請檢查它是否有幫助。 –

+3

這是一個更好的鏈接,我認爲(到當前主幹):http://selenium.googlecode.com/svn/trunk/docs/api/dotnet/html/M_Selenium_ISelenium_GetAlert.htm –

0

我發現在Selenium RC中,如果您知道會有一個確認按鈕,那麼您需要需要selenium.getConfirmation();添加到您的代碼中。

下面是我用它(注意:我在Eclipse中使用Java)

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 

希望這有助於!

相關問題