Home » Java » Java Jsoup Submitting a form-Exceptionshub

Java Jsoup Submitting a form-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to figure out how to submit a form using Jsoup.
On Xfinity’s website, I am trying to input an address and get back the resulting page after clicking on “Show me deals” from the url below:

https://www.xfinity.com/learn/offers

Here is my current code:

    public String getISP() throws IOException {
    Connection.Response addressFormResponse = Jsoup.connect("https://www.xfinity.com/learn/offers")
            .data("Address.SingleStreetAddress", address)
            .method(Connection.Method.POST)
            .execute();

    Document doc = addressFormResponse.parse();

    System.out.println(doc.title());
    System.out.println(doc.location());
    if (doc.location().contains("Active Address")) {
        return "Comcast XFinity";
    }
    return "Cannot find an ISP";
}

The current code only returns the same webpage, how would I get back the resulting page?

How to&Answers:

Jsoup is a HTML parser library, it provides functionality for extracting and manipulating data on HTML page. If you need traverse websites, submit forms, click elements, it’s better to use another tools, like selenium, HTTP client (which are often used for automated test of web applications) or web crawler libraries like crawler4j.

Answer:

I would tend to disagree with Daniil’s answer in that neither HTTP Client or crawler4j support javascript which is required for this page. Selenium is probably the best solution.

What follows is an example of how to use jsoup to fetch a page, fill out a form, and submit it. The result is json and so you would then pass that string to gson or similar. I did not that the page was very flaky just in a regular browser, and sometimes would catch the address input and sometimes would barf on the same input.

Document doc = Jsoup.connect("https://www.xfinity.com/learn/offers").get();
FormElement form = (FormElement) doc.selectFirst("[data-form-dealfinder-localization]");
Element input = form.selectFirst("#Address_StreetAddress");
input.val("2000 YALE AVE E, SEATTLE, WA 98102");
String json = form.submit().ignoreContentType(true).execute().body();

System.out.println(json);