开发者

Htmlunit : filling a form that refreshes automatically

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 开发者_如何学GoCountry (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.


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... ;-)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜