Question

I have used HTTPClient to connect to a website and I can successfully access required data from the website using jsoup. I have the following code from which I need to extract the submit button info.

<form method="POST" action="test.jsp" >
         <font size="2">
         <input type="hidden" name="num" id="num" value=123 >
         <input type="hidden" name="iec" id="iec" value=456 >
         <input type="submit" onclick=" return check();" value="Print" name="B1">
         </font>
        </form>

How can I access the value and name of the submit button?

Was it helpful?

Solution

You can access those values using the attr(String attribute) method of Element. For example:

String html = "<form method=\"POST\" action=\"test.jsp\" >"
        + "<font size=\"2\">"
        + "<input type=\"hidden\" name=\"num\" id=\"num\" value=123 >"
        + "<input type=\"hidden\" name=\"iec\" id=\"iec\" value=456 > "
        + "<input type=\"submit\" onclick=\" return check();\" value=\"Print\" name=\"B1\">"
        + "</font>"
        + "</form>";

Document doc = Jsoup.parse(html);

Element bttn = doc.select("input[type=submit]").first();

String value = bttn.attr("value"); // will be Print
String name =  bttn.attr("name");  // will be B1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top