[英]JSoup: How to remove an element from a Form?
我使用 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
調用Node#remove
時,它會更新父Node#remove
的子Node#remove
列表,而不是內部列表。
因此,要真正刪除一個元素,您還需要從這個內部列表中刪除它:
e.select("input[name=buttonBack]").remove();
form.elements().removeIf(e -> e.attr("name").equals("buttonBack"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.