HTMLUnit无法在onchangeEvent上触发ajaxCall

时间:2018-02-01 12:53:41

标签: java htmlunit

外部网站上有两个HtmlSelects。

第一个("句号")有静态属性。但是第二个(" date")处于禁用状态,并且在加载页面时没有选项。 (" date")字段被启用,并且在选择(" period")时填充数据。 通过HTMLUnit执行时,此功能不起作用。

下面提到的是我的代码

WebClient webClient = new WebClient();
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setCssEnabled(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page = webClient.getPage(PAGE_URL);
HtmlSelect select = (HtmlSelect) page.getElementById("period");
HtmlOption option = select.getOptionByText("Q1");
select.setSelectedAttribute(option, true);  select.fireEvent(Event.TYPE_CHANGE);
Thread.sleep(20000);
HtmlSelect select1 = (HtmlSelect) page.getElementById("date");
HtmlOption option1 = select1.getOptionByValue("2"); //EXCEPTION IN THIS LINE
select1.setSelectedAttribute(option1, true);

以下是捕获的异常

com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[option] attributeName=[value] attributeValue=[2]
    at com.gargoylesoftware.htmlunit.html.HtmlSelect.getOptionByValue(HtmlSelect.java:437)

似乎(" date")字段未启用并填充。

非常感谢任何帮助。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

Without more details it is really tricky to give some advice.

Please report:

  • the version of HtmlUnit you are using
  • the PAGE_URL you are using to give us the option to reproduce your case; if your page is private maybe you are using one of these webframeworks and there is a good chance to find a demo page to show your problem
  • check the page code and make sure the trigger of the change event is really the select
  • There is no need to trigger the change event, This is done by HtmlUnit alreade; replace select.setSelectedAttribute(option, true); select.fireEvent(Event.TYPE_CHANGE); with option.setSelected(true);
  • try to figure out if the ajax requests gets triggered by using a http proxy like Charles or by enabling HttpClient wire logging

Hope that helps, if you need more support provide a complete reproducible sample.

Regarding your version: version 2.13 is really outdated and of course we did many fixes for event handling/processing and also for the select control. Please use the latest version - this is the only version we can support because the development team is really small.