jsoup android java.lang.RuntimeException:无法找到表单

时间:2017-09-26 18:52:20

标签: java android jsoup

我尝试使用JSOUPfood delivery website获取数据。目标是填写address field,点击submit button并显示对用户的回复。但是当我试图在主页上找到该表单时,我得到 java.lang.RuntimeException: Unable to find form searchFormResponse.parse()包含主页HTML,因此问题不存在。我已经尝试了多种方法从jsoup网站中选择表单,但似乎没有任何效果。任何帮助都感激不尽。如果它有任何重要性,它适用于Android应用程序。提前致谢。

这是我的代码

        final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
        final String LOGIN_FORM_URL = "https://deliveroo.fr/fr/"; //TODO make it adapt to the country
        final String myAddress = a;

// # Go to login page
        Connection.Response searchFormResponse = Jsoup.connect(LOGIN_FORM_URL)
                .method(Connection.Method.GET)
                .userAgent(USER_AGENT)
                .execute();

// # Fill the search form
// ## Find the search first...
        FormElement searchForm = (FormElement)searchFormResponse.parse()
                .select("div#landing-index-page-search__container > form").first();

// ## ... then "type" the address ...
        Element searchField = searchForm.select("#landing-index-page-search--input").first();
searchField.val(myAddress);


// # Now send the form
        Connection.Response loginActionResponse = searchForm.submit()
                .cookies(searchFormResponse.cookies())
                .userAgent(USER_AGENT)
                .execute();
    System.out.println(loginActionResponse.parse().html());

部分网站代码

<div class="landing-index-page-search__container" data-reactid=".1d1c1t7z20w.2.0.0.1.1">
    <h1 class="landing-index-page-search--main-title" data-reactid=".1d1c1t7z20w.2.0.0.1.1.0">Vos restaurants préférés, livrés en moins de 30 minutes.</h1>
    <form method="get" action="" class="landing-index-page-search--form landing-index-page-search--non-postcode" data-reactid=".1d1c1t7z20w.2.0.0.1.1.2">
    <span data-reactid=".1d1c1t7z20w.2.0.0.1.1.2.0"></span>
    <div data-reactid=".1d1c1t7z20w.2.0.0.1.1.2.1">
    <div class="landing-index-page-search--input" data-reactid=".1d1c1t7z20w.2.0.0.1.1.2.1.1">
    <div class="landing-index-page-search--input address-search" data-reactid=".1d1c1t7z20w.2.0.0.1.1.2.1.1.1">
    <input name="address_search" type="text" tabindex="-1" class="" placeholder="Saisissez votre adresse" value=" " data-reactid=".1d1c1t7z20w.2.0.0.1.1.2.1.1.1.0"/></div>
    <input id="find_food" type="submit" value="Voir les restaurants" class="button" data-reactid=".1d1c1t7z20w.2.0.0.1.1.2.1.1.2"/>

1 个答案:

答案 0 :(得分:1)

我想问题是,您尝试选择带有 id <div>的{​​{1}}行

landing-index-page-search__container

但据我所知,只有FormElement searchForm = (FormElement)searchFormResponse.parse() .select("div#landing-index-page-search__container > form").first(); <div>

选择应该是

landing-index-page-search__container

区别在于,您通过FormElement searchForm = (FormElement)searchFormResponse.parse() .select("div.landing-index-page-search__container > form").first(); (点)选择了,您可以选择id元素。

要仅按类选择元素,而不知道/提及您可以使用的标记:

class