Htmlunit : filling a form that refreshes automatically

被刻印的时光 ゝ 提交于 2019-12-07 15:46:07

问题


I can normally fill-in a form using an HtmlUnit and submit it to the server. Fine.

UnfortunateIy I don't know how to fill-in a specific form that refreshes to server depening on values I set on the form. That form seems to refreshes to the server because of some javascript that is triggered when a value is set for a field...

To make a simple example, I have a form with 2 fields, drop-down lists, let's say Country and Region: when I choose a value from field Country, the page automatically refreshes to the server (javascript) and shows the same form with field Region updated with possible values dependent on previous choice for field Country.

Here is an online test page with a form I am talking about: http://web1.ciemme-service.it/html-unit.nsf/form?OpenForm

What I would like to do is: - load the page with the form I have to fill-in (works) - set a value for field Country (works) - wait that page refreshes to the server (done by javascript automatically...) - set a value for field Region - submit the form

Thanks for any help/hints you can give me.


回答1:


I guess I found the solution: actually I have to think to WebClient object as a real web browser!

After I set the value for Country field, I wait some seconds for the refresh to happen (javascript), then I get the current HtmlPage from the webClient object (the refreshed page!) and continue working on the new page (set a value for Regione field + click on the submit button):

Here's an example:

final HtmlSelect text_nazione = form.getSelectByName("nazione");
text_nazione.setSelectedAttribute("italy", true); 

// now onchange event triggered and round-trip to server

wc.waitForBackgroundJavaScript(3000);

currentPage= (HtmlPage) wc.getCurrentWindow().getEnclosedPage(); 
form = currentPage.getFormByName("_form");
final HtmlSelect text_regione= form.getSelectByName("regione");

text_regione.setSelectedAttribute("I2", true);

tmlButtonInput button = form.getElementById("invia");
currentPage = (HtmlPage) button.click();

Hope it helps! More testing ongoing... ;-)




回答2:


you might want to take a look at this page which shows how you can wait for ajax response

see also this question on a similar topic



来源:https://stackoverflow.com/questions/7203673/htmlunit-filling-a-form-that-refreshes-automatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!