【问题标题】:Iterating over form elements htmlunit遍历表单元素htmlunit
【发布时间】:2014-09-12 23:27:56
【问题描述】:

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

非常感谢

【问题讨论】:

    标签: java forms element htmlunit


    【解决方案1】:

    来自文档:

    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:不,这不可能,您可以使用 XPath(用于解析 xml(html 是 xml)的 java 库)来获得您想要的效果。

    祝你好运:)

    【讨论】:

      【解决方案2】:

      您可以通过多种方式进行迭代,例如选择行并对其进行迭代

          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
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-04
        • 1970-01-01
        • 2016-03-06
        • 2013-11-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多