Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在表单元素htmlunit上迭代_Java_Forms_Element_Htmlunit - Fatal编程技术网

Java 在表单元素htmlunit上迭代

Java 在表单元素htmlunit上迭代,java,forms,element,htmlunit,Java,Forms,Element,Htmlunit,你知道htmlunit是否支持表单元素返回的表单迭代吗。 我可以通过使用getInputByName来访问特定的元素,如果元素名不存在,甚至可以使用该值。但是,例如,如何在列表中获取特定表单的所有元素呢。这是可能的还是我需要自己解析表单 非常感谢这些文档: XPath is the suggested way for more complex searches, a brief tutorial can be found in W3Schools @Test public void xpath

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

非常感谢这些文档:

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();
}
XPath是更复杂搜索的建议方式,可以在W3Schools中找到一个简短的教程
@试验
public void xpath()引发异常{
最终WebClient WebClient=新WebClient();
最终HtmlPage=webClient.getPage(“http://htmlunit.sourceforge.net");
//获取所有div的列表
最终列表divs=page.getByXPath(“//div”);
//get div的'name'属性为'John'
最终的HtmlDivision div=(HtmlDivision)page.getByXPath(“//div[@name='John'])。获取(0);
webClient.closeAllWindows();
}
所以:不,这是不可能的,您可以使用XPath(用于解析xml(html是xml)的java库)来获得所需的效果


祝您好运:)

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

    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
    }
HtmlTable table=(HtmlTable)文件\u page.getByXPath(
“//表[@class='GDOGFYVLF']”。获取(0);
java.util.List table_rows=table.getRows();
对于(int i=1;i