2014-09-12 87 views
1

你知道htmlunit是否支持表单元素返回的表单迭代。 我可以通过使用getInputByName来获取特定元素,或者如果元素名称不存在,甚至可以使用该值。但是如何获取列表中特定表单的所有元素。是可能的,或者我需要解析的形式自己迭代表单元素htmlunit

非常感谢

回答

0

从文档:

XPath is the suggested way for more complex searches, a brief tutorial can be found in W3Schools 

@Test 
public void xpath() throws Exception { 
    final WebClient webClient = new WebClient(); 
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net"); 

    //get list of all divs 
    final List<?> divs = page.getByXPath("//div"); 

    //get div which has a 'name' attribute of 'John' 
    final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']").get(0); 

    webClient.closeAllWindows(); 
} 

SO:不,它不是possiable,您可以使用XPath(一个Java库用于解析xml(html是xml))来获得您想要的效果。

祝你好运:)

0

可以在例如选择行并遍历他们

HtmlTable table = (HtmlTable) filings_page.getByXPath(
      "//table[@class='GDOGFYVLF']").get(0); 
    java.util.List<HtmlTableRow> table_rows = table.getRows(); 
    for (int i = 1; i < table.getRows().size(); i++) { 
     //your logic 
    } 
多种方式迭代