FormElement extends Element { public List<Connection.KeyVal> formData() { ArrayList<Connection.KeyVal> data = new ArrayList<Connection.KeyVal>(); for (Element el: elements) { if (!el.tag().isFormSubmittable()) continue; String name = el.attr("name"); if (name.length() == 0) continue; if ("select".equals(el.tagName())) { Elements options = el.select("option[selected]"); for (Element option: options) { data.add(HttpConnection.KeyVal.create(name, option.val())); } } else { data.add(HttpConnection.KeyVal.create(name, el.val())); } } return data; } FormElement(Tag tag, String baseUri, Attributes attributes); Elements elements(); FormElement addElement(Element element); Connection submit(); List<Connection.KeyVal> formData(); @Override boolean equals(Object o);  }
@Test public void createsFormData() { String html = "<form><input name='one' value='two'><select name='three'><option value='not'>" + "<option value='four' selected><option value='five' selected><textarea name=six>seven</textarea></form>"; Document doc = Jsoup.parse(html); FormElement form = (FormElement) doc.select("form").first(); List<Connection.KeyVal> data = form.formData(); assertEquals(4, data.size()); assertEquals("one=two", data.get(0).toString()); assertEquals("three=four", data.get(1).toString()); assertEquals("three=five", data.get(2).toString()); assertEquals("six=seven", data.get(3).toString()); }