2

I need to parse HTML elements from a webpage in Java, fill out a form and submit it to simulate a login.

I've been doing a little bit of research and here's what I did using the jsoup library.

However, after implementing this, I realized jsoup is only for parsing and can't perform button click, for example.

In this example, I need to perform a click on submitButton element.

try {
    Document htmlDoc = Jsoup.connect(MY_URL).get();
    Element loginForm = htmlDoc.getElementById("login-form");
    Element usernameField = loginForm.getElementById("username");
    Element passwordField = loginForm.getElementById("passwd");
    Element submitButton = loginForm.getElementById("submit");

    usernameField.val(username);
    passwordField.val(password);

    // I need to simulate `submitButton` click so I can login

} catch (Exception e) {
    Log.e("TAG", e.toString());
}

Am I correct in my understanding that jsoup is not capable of doing this?

If so, is there a library for Android that can?

I understand there are many Java libraries but most can't work with Android (ex. HtmlUtil) due to javax limitation.

9
  • Do you want to emulate a browser or do you just want to post a form? Why would you need to parse the form at all? After all, you are extracting no information from the form in the code snippet you supplied. Commented Nov 19, 2016 at 3:06
  • I don't need to extract any info. All I need to do is verify if the provided username and password are valid. If I can simulate a click, i can verify if the provided id and password are valid or not. I know there are better methods to do this however I need to implement it this way temporarily Commented Nov 19, 2016 at 3:10
  • The approach heavily depends on the nature of the site you want to probe. If it returns an error (404, 403, 401 for example), you can just post a username and password and look at the response. See developer.android.com/reference/java/net/HttpURLConnection.html for getting on the right track. If you search for HttpURLConnection and HTTP POST, you will get plenty of tutorials on how to POST form data to a URL. Commented Nov 19, 2016 at 3:14
  • You need to find the target link and parameters that needs to be passed. You don't need to parse the page. Commented Nov 19, 2016 at 4:24
  • I already have the target link and parameters. Based on the the answer given below (link), I tried however I am getting the following response: "The most recent request was denied because it contained an invalid security token. Please refresh the page and try again." when I do the post method Commented Nov 19, 2016 at 4:36

1 Answer 1

1

You can view this link. Provides a good answer.

The steps should be the following:

  1. Get to the login page
  2. Parse the html using jsoup for username and password
  3. Replace it with the actual username and password
  4. Use an http POST to post the username and password form.
Sign up to request clarification or add additional context in comments.

5 Comments

Looks promising. I will check it out. Thanks
I tried this example however even when I enter the wrong credentials, it returns a 200. On a browser, it returns 303 so something doesn't seem right
I even tried doing the same exact task using a chrome extension called Postman and that returns a 200 as well. I want to know when user doesn't enter the right password. 303 is the response I am looking for so I can differentiate between successful and fail login attempts
@th3pat3l Did that help you?
Unfortunately no it did not. I was very hopeful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.