How to fill a form with Jsoup?

前端 未结 2 645
情话喂你
情话喂你 2020-11-30 11:51

I am trying to navigate to description page of California website http://kepler.sos.ca.gov/. but unable to go .

Then,I have a html form, on which I am submitting re

2条回答
  •  醉话见心
    2020-11-30 12:22

    This is the exact same code as posted above in the accepted answer, except that it reflects the changes California made to their website after the original answer was posted. So as of my writing this, this code works. I've updated original comments, identifying any changes.

    // * Connect to website (Orignal url: http://kepler.sos.ca.gov/)
    String url = "https://businesssearch.sos.ca.gov/";
    Connection.Response resp = Jsoup.connect(url) //
                                    .timeout(30000) //
                                    .method(Connection.Method.GET) //
                                    .execute();
    
    // * Find the form (Original jsoup selector: from#aspnetForm)
    Document responseDocument = resp.parse();
    Element potentialForm = responseDocument.select("form#formSearch").first();
    checkElement("form element", potentialForm);
    FormElement form = (FormElement) potentialForm;
    
    // * Fill in the form and submit it
    // ** Search Type (Original jsoup selector: name$=RadioButtonList_SearchType)
    Element radioButtonListSearchType = form.select("name$=SearchType]").first();
    checkElement("search type radio button list", radioButtonListSearchType);
    radioButtonListSearchType.attr("checked", "checked");
    
    // ** Name search (Original jsoup selector: name$=TextBox_NameSearch)
    Element textBoxNameSearch = form.select("[name$=SearchCriteria]").first();
    checkElement("name search text box", textBoxNameSearch);
    textBoxNameSearch.val("cali");
    
    // ** Submit the form
    Document searchResults = form.submit().cookies(resp.cookies()).post();
    
    // * Extract results (entity numbers in this sample code, orignal jsoup selector: id$=SearchResults_Corp)
    for (Element entityNumber : searchResults.select("table[id$=enitityTable] > tbody > tr > td:first-of-type:not(td[colspan=5])")) {
        System.out.println(entityNumber.text());
    }
    

提交回复
热议问题