HTMLUnit:填写自动刷新的表格 [英] Htmlunit : filling a form that refreshes automatically

查看:53
本文介绍了HTMLUnit:填写自动刷新的表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常可以使用HtmlUnit填写表格并将其提交给服务器.好吧.

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

不幸的是,我不知道如何填写特定的表格,该表格会根据我在表格上设置的值刷新到服务器.该表单似乎刷新到服务器,因为在为字段设置值时触发了一些javascript ...

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

举一个简单的例子,我有一个包含2个字段的表格,下拉列表,例如Country和Region:当我从Country字段中选择一个值时,页面会自动刷新到服务器(javascript)并显示与字段区域"相同的表格,其更新的可能值取决于字段国家/地区"的先前选择.

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.

这是一个在线测试页面,其中包含我正在谈论的表单: http://web1.ciemme-service.it/html-unit.nsf /form?OpenForm

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

我想做的是: -使用我必须填写的表格加载页面(有效) -为国家/地区"字段设置一个值(有效) -等待该页面刷新到服务器(由javascript自动完成...) -为字段Region设置一个值 -提交表格

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.

推荐答案

我想我找到了解决方案:实际上,我必须将WebClient对象视为真正的Web浏览器!

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

设置国家/地区"字段的值后,等待几秒钟以进行刷新(javascript),然后从webClient对象(刷新的页面!)获取当前的HtmlPage,然后继续在新页面上工作(设置区域字段的值+点击提交按钮):

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):

这是一个例子:

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

这篇关于HTMLUnit:填写自动刷新的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆