Reputation: 521
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.
Upvotes: 1
Views: 11261
Reputation: 1057
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.
Upvotes: 1
Reputation: 27486
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();
Upvotes: 2