最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I need to read the text displayed on the popup window in Web driver using Java - Stack Overflow

programmeradmin7浏览0评论

I need to read the text displayed on the popup window in Web driver using Java. I am able to handle the popup window for closing. I don't know how to read the text displayed on Popup window and print it in Console.

I am not able to provide any HTML code for this because its a Modal Popup window.

Please help me on this. Help will be appreciated.

I need to read the text displayed on the popup window in Web driver using Java. I am able to handle the popup window for closing. I don't know how to read the text displayed on Popup window and print it in Console.

I am not able to provide any HTML code for this because its a Modal Popup window.

Please help me on this. Help will be appreciated.

Share Improve this question asked Feb 27, 2013 at 6:45 Umamaheshwar ThotaUmamaheshwar Thota 5213 gold badges14 silver badges32 bronze badges 4
  • if this is your application, shouldn't you be able to capture the message you're gonna display on the pop-up? – Rahul Commented Feb 27, 2013 at 6:46
  • Thanks for your early reply. It is the application i am working on but message displayed on Popup window is not able to read because it is a modal popup window not a normal popup where we can work without closing the popup window. without closing the Modal Popup window, we cannot work on the application anymore. – Umamaheshwar Thota Commented Feb 27, 2013 at 6:55
  • And that popup - is it javascript popup? – Pavel Janicek Commented Feb 27, 2013 at 7:55
  • I found this describing how to get the text of an alert box: – VolkerK Commented Feb 27, 2013 at 7:57
Add a ment  | 

2 Answers 2

Reset to default 2

Given your screenshot, it looks like the "modal popup" you're trying to automate is generated by the JavaScript alert() function. If this is the case, the following code or something similar to it, should work.

// WARNING! Untested code written from memory without
// benefit of an IDE! May not be exactly correct!

// Switch the driver context to the alert
Alert alertDialog = driver.switchTo().alert();

// Get the alert text
String alertText = alertDialog.getText();

// Click the OK button on the alert.
alertDialog.accept();

Have you used the WebDriverWait object before? To expand on the previous answer, you may be able to do something similar to this, but I have not tested:

WebDriverWait wait = new WebDriverWait(5, TimeUnit.Seconds);

element.click();

// Wait for the dialog to show
wait.until(ExpectedConditions.alertIsPresent());

// Switch the driver context to the alert
Alert alertDialog = driver.switchTo().alert();

// Get the alert text
String alertText = alertDialog.getText();

// Click the OK button on the alert.
alertDialog.accept();

Also, you may have to switch back to the alert after getting the text. Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论