Jsoup to connect to a "Javascript" link

338 Views Asked by At

Consider the following link:

<a id="login" class="js-check" rel="nofollow" data-nop="" ref="javascript:void(0)">Login</a>

Through Jsoup, I can retrieve this element as follows:

Element bodyElement = doc.body();
Element A_Login = bodyElement.getElementById("login");

Now, I would like to follow this link with Jsoup and I'm no sure how I can do this?

FWIW, in the browser, clicking this link yields a registration/login form:

<form id="login_form" method="post" action="https://www.link.com/forum/login/">
  <table class="formTable">
    <tbody>
      <tr>
        <td><label class="required" for="login_login">Login</label></td>
        <td><input id="login_login" type="text" required="required" name="login[login]"> </td>
      </tr>

      <tr>
        <td><label class="required" for="login_password">Password</label></td>
        <td><input id="login_password" type="password" required="required" name="login[password]"></td>
      </tr>

      <tr>
        <td><button type="submit">Login</button></td>
     </tr>
  </tbody>
</table>
</form

Of course, the above login form is asynchronously loaded, and not part to the "base" HTML contents..

How can I use Jsoup to login in such situations?

1

There are 1 best solutions below

2
On

Jsoup does not interpret Javascript. So you either need to analyze the script yourself and create appropriate urls that the simple JSoup http client can load, or you can use a full browser to get the link. Selenium http://www.seleniumhq.org/docs/03_webdriver.jsp in combination with phantomjs http://phantomjs.org/ are are a good solution for that.