Search code examples
javascriptandroidhtmlweb-scrapingjsoup

How to deal with missing name attribute from Input tag when using Jsoup to login?


this is what I am trying to do but I don't know how to add the login button to formData hashMap

 private fun getData() {
    Log.d(TAG, "getLoginPage: ")
    try {
        val loginFormResponse=Jsoup.connect(getString(R.string.loginUrl))
            .method(Connection.Method.GET)
            .userAgent("Mozilla")
            .execute()

      val document=loginFormResponse.parse()
        val cookies: HashMap<String, String> = HashMap(loginFormResponse.cookies())
        Log.d(TAG, "getData: ------------------------------${cookies["__RequestVerificationToken"]}")
        val authToken:String=cookies["__RequestVerificationToken"]!!
        val formData: HashMap<String, String> = HashMap()
        formData["utf8"] = "e2 9c 93";
        formData["Email"] ="[email protected]"
        formData["Password"] ="tttt"
        formData["__RequestVerificationToken"] = authToken
        //Missing login button here

        val homePage = Jsoup.connect(getString(R.string.loginUrl))
            .cookies(cookies)
            .data(formData)
            .method(Connection.Method.POST)
            .userAgent("Mozilla")
            .execute()

        Log.d(TAG, "getData: ---------------------------------------$homePage")
    }
    catch (E:IOException){
        Log.d(TAG, "getLoginPage: ----IO EXCEPTION----${E.message}")
    }
}

This is the website I am trying to use Please help!


Solution

  • You take authToken from cookies

    val authToken:String=cookies["__RequestVerificationToken"]!!
    

    but you should take it from the input in that form because there's another hidden input with different value.
    So try this:

    val authToken:String = document.select("input[name=__RequestVerificationToken]").first().attr("value")
    

    And you don't have to add the button to submitted data.