4

I am working on HTML unit with Java to read HTML pages. I have a scenario where I have to read messages from the popup/ alert window. I have an index page page = form.getInputByName("index").click();

After I click on the index page I get the response page. But before that I get an alert some thing like

enter image description here

I want to read the above message and then proceed with ok.

I tried with alert handlers like `

ConfirmHandler okHandler = new ConfirmHandler(){
                @Override
                  public boolean handleConfirm(Page page, String message) {
                      System.out.println(">>>>>>>>>>>>>>>>>>> message--"+message);
                         return true;
                            }


            };
            webClient.setConfirmHandler(okHandler);`

But this is not working for me.

halfer
  • 19,824
  • 17
  • 99
  • 186
Shaik Mujahid Ali
  • 2,308
  • 7
  • 26
  • 40

1 Answers1

4

You should be using the CollectingAlertHandler instead:

CollectingAlertHandler alertHandler = new CollectingAlertHandler();
webClient.setAlertHandler(alertHandler);

/*Your browsing codes here*/

List<String> alertmsgs = new ArrayList<String>();   
alertmsgs = alertHandler.getCollectedAlerts();

Then you can use the message obtained as you want. Reminder: You do not need to click the OK button.

PSo
  • 958
  • 9
  • 25
  • If you have an "Ok" / "Cancel" alert, how do you specify which button to click? How do you get the resulting page after you dismiss the alert? – Dale Mar 16 '22 at 18:27