0

I've used HtmlUnit to send a login through a form before and I've been trying to do it again but just can't get it to work on this site: http://www.runescape.com/companion/comapp.ws

Using the code

final HtmlPage page1 = webClient.getPage("http://www.runescape.com/companion/comapp.ws"); 
final HtmlForm form = (HtmlForm) page1.getForms().get(0);`

Returns nothing, even though when you inspect element on the page the form:

<form name="loginForm" ng-submit="login()" autocomplete="off" class="ng-pristine ng-invalid ng-invalid-required"> ... </form> clearly exists.

Also the code

final HtmlPage page1 = webClient.getPage("http://www.runescape.com/companion/comapp.ws");
final HtmlTextInput textField = page1.getFirstByXPath("//input[@id='username']");

and

final HtmlPage page1 = webClient.getPage("http://www.runescape.com/companion/comapp.ws");
final HtmlTextInput textField = page1.getElementByName("username");

both return nothing despite there being an input field named "username" with the id of "username"

Edit:

<form name="loginForm" ng-submit="login()" autocomplete="off" class="ng-pristine ng-invalid ng-invalid-required">
    <button type="submit" class="icon-login" ng-disabled="!loginForm.$valid" disabled="disabled"></button>
    <input type="text" name="username" id="username" placeholder="Username / Email" autocapitalize="off" autocorrect="off" required="" ng-model="credentials.username" class="ng-pristine ng-invalid ng-invalid-required" data-emoji_font="true" style="font-family: MuseoSans500, sans-serif, 'Segoe UI Emoji', 'Segoe UI Symbol', Symbola, EmojiSymbols !important;">
    <input type="password" name="password" id="password" placeholder="Password" autocapitalize="off" autocorrect="off" required="" ng-model="credentials.password" class="ng-pristine ng-invalid ng-invalid-required">
    </form>

I'm not sure if I'm going about this wrong, or this method just wont work on this particular page for some reason that I'm not seeing. A fix to my code, or any alternative methods of logging in are much appreciated. Thanks!

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
user2288508
  • 45
  • 3
  • 9
  • what is your angular js login method code (ng-submit="login()"), please post that, that may be helpfull to answer you. – user3278897 Jul 15 '15 at 23:37
  • @user3278897 I'm not sure what you mean by "angular js login method code" – user2288508 Jul 15 '15 at 23:41
  • @user3278897 I've added the entire
    code to the edit of my main post. I assume that's what you wanted
    – user2288508 Jul 15 '15 at 23:47
  • @user3278897 your link is giving me a 404 error – user2288508 Jul 15 '15 at 23:57
  • sorry, please check this: http://jsfiddle.net/smileofninja/ad7rky05/2/ – user3278897 Jul 16 '15 at 00:00
  • @user3278897 I'm not sure you understand my question. What I want to do is login to a website using the HtmlUnit class in java. The code example I provided in my edit is the code from using inspect element on the website I want to login to. – user2288508 Jul 16 '15 at 00:09
  • Yes i known you are using HtmlUnit but, my point is you are using angular js also. Please note that, till the HtmlUnit (2.13 version), Html unit with angularJS has problems, check this link: http://sourceforge.net/p/htmlunit/bugs/1559/. To be honest, i might not be much helpful here, check this link too, who might have same problem as you and they solved it: http://www.widecodes.com/7zJmWWPeVW/htmlunit-not-working-with-angularjs.html – user3278897 Jul 16 '15 at 00:18

1 Answers1

0

You can always construct the httpwebrequest and login that way.

Arya
  • 8,473
  • 27
  • 105
  • 175