java - JSoup:如何从表单中删除元素?

标签 java jsoup

我使用jsoup来解析html页面并提交表单。我需要在提交表单之前删除“后退”按钮。我使用 element.remove() 方法,但后来我发现 form.formData() 没有改变。请求的元素已从 form.children() 中删除,但存在于 form.elements() 中。这是一个错误还是我使用了错误的方法从表单中删除元素?

public class JsoupCheck {
    public static void main(String[] args) {
        String html = "<html><body><form action=\"demo\">"
                + "<input type=\"submit\" name=\"buttonSave\" value=\"Save\">"
                + "<input type=\"submit\" name=\"buttonBack\" value=\"Back\">"
                + "<select name=\"selection\">"
                + "  <option value=\"value1\">Value 1</option>"
                + "  <option value=\"value2\" selected>Value 2</option>"
                + "  <option value=\"value3\">Value 3</option>"
                + "</select>"
                + "</form></body></html>";
        Document doc = Jsoup.parse(html);
        FormElement form = (FormElement) doc.select("form").first();
        Element e = form.select("form").first();

        System.out.println("=== Original content of form");
        System.out.println(e);
        System.out.println("=== Original content of form.formData()");
        for (Connection.KeyVal i : form.formData()) {
            System.out.println(i.key() + "=" + i.value());
        }
        System.out.println("form.elements().size() = " + form.elements().size());
        System.out.println("form.children().size() = " + form.children().size());

        e.select("input[name=buttonBack]").remove();
        System.out.println();

        System.out.println("=== Content of form after remove buttonBack (result: buttonBack removed)");
        System.out.println(e);
        System.out.println("=== Content of form.formData() after remove buttonBack (result: buttonBack exist)");
        for (Connection.KeyVal i : form.formData()) {
            System.out.println(i.key() + "=" + i.value());
        }
        System.out.println("form.elements().size() = " + form.elements().size());
        System.out.println("form.children().size() = " + form.children().size());
    }
}

输出是:

=== Original content of form
<form action="demo">
 <input type="submit" name="buttonSave" value="Save">
 <input type="submit" name="buttonBack" value="Back">
 <select name="selection"> <option value="value1">Value 1</option> <option value="value2" selected>Value 2</option> <option value="value3">Value 3</option></select>
</form>
=== Original content of form.formData()
buttonSave=Save
buttonBack=Back
selection=value2
form.elements().size() = 3
form.children().size() = 3

=== Content of form after remove buttonBack (result: buttonBack removed)
<form action="demo">
 <input type="submit" name="buttonSave" value="Save">
 <select name="selection"> <option value="value1">Value 1</option> <option value="value2" selected>Value 2</option> <option value="value3">Value 3</option></select>
</form>
=== Content of form.formData() after remove buttonBack (result: buttonBack exist)
buttonSave=Save
buttonBack=Back
selection=value2
form.elements().size() = 3
form.children().size() = 2

最佳答案

FormElement 是一种特殊类型的节点。除了维护所有子元素的列表(继承自 Node)之外,它还保存表单内所有元素的第二个内部列表。

public class FormElement extends Element {
    private final Elements elements = new Elements();
    ...
}

当您对子级调用 Node#remove 时,它会更新父级的子级列表,而不是内部列表。

因此,要真正删除一个元素,您还需要将其从这个内部列表中删除:

e.select("input[name=buttonBack]").remove();
form.elements().removeIf(e -> e.attr("name").equals("buttonBack"));

关于java - JSoup:如何从表单中删除元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46482697/

相关文章:

java - 我可以在 Windows 64 位平台上安全使用的最大 Java 堆大小是多少?

java - 在 JUnit 切换到 TestNG 后我的测试不起作用

java - 为什么 LDAP 身份验证例程通常使用两个绑定(bind)?

java - 单行文本 block

java - 使用 Jsoup 登录 Google 帐户

java - 将对象从 servlet 传递到客户端(javascript)

css - Jsoup 选择元素

java - 数字字符引用的 JSoup 编码问题

java - 获取jsoup中html字符串中的所有属性

java - 提取 HTML 标记外的文本