Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read alerts in HTML unit

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.

like image 632
Shaik Mujahid Ali Avatar asked Oct 28 '25 10:10

Shaik Mujahid Ali


1 Answers

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.

like image 50
PSo Avatar answered Oct 31 '25 01:10

PSo