0

i want to access a webpage of a local appliance. This is a part of the HTML code of that page:

<div class="form-container">
    <h2 class="form-signin-heading">Please Log In</h2>
    <div class="form-group input-group auth focus">
      <label for="user_account" class="visuallyhidden">Username:</label>
      <span class="input-group-addon"><i class="icon-user"></i></span>
      <input autofocus="autofocus" class="form-control auth" id="user_account" maxlength="128" name="user[account]" placeholder="Enter User Name" size="128" type="text" />
    </div>
    <div class="form-group input-group auth">
      <label for="user_password" class="visuallyhidden">Password:</label>
      <span class="input-group-addon"><i class="icon-lock"></i></span>
       <input autocomplete="off" class="form-control auth" id="user_password" maxlength="128" name="user[password]" placeholder="Enter Password" size="128" type="password" />
    </div>
    <input type="hidden" id="window_size" name="window_size"/>
    <input type="hidden" id="url_hash" name="url_hash"/>
    <button id="logInButton" class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
  </div>

I want to enter a username and a password and then login via the login button. The script worked before i update the appliance. Now i cant get it to work again.

This is my script:

page.open("https://10.84.163.146/login/login", function(status){
    console.log(page.content);

});

page.render('Bild2.png');
console.log(page.content);
page.evaluate(function(){
    console.log(document.getElementsByClassName("form-group input-group auth"));
    console.log(document.getElementById("user_account"));
    document.getElementsByClassName("form-control auth").value="op";

The console output is this:

/Desktop$ phantomjs --ignore-ssl-errors=true check.js 
<html><head></head><body></body></html>
[object NodeList]
null

So when printing the page.content it is empty, and the rendered picture is also blank. But if it can't find/open the page, why does if find a [object NodeList] but not the user_account element?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Helyx
  • 329
  • 1
  • 5
  • 17
  • What PhantomJS version are you using? Please register to the `onConsoleMessage`, `onError`, `onResourceError`, `onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file-1_phantomerrors-js)). Maybe there are errors. – Artjom B. Oct 27 '15 at 13:43
  • i'm using 1.9 since 2.0 doesnt want to install correctly. When using your code it says "Unable to load resource (#undefinedURL:https://10.84.163.146/login/login) Error code: 6. Description: SSL handshake failed Status: fail ". but why doesnt it work. i have the igone-ssl-errors option enabled – Helyx Oct 29 '15 at 12:40
  • It's probably because of Poodle. See [PhantomJS failing to open HTTPS site](http://stackoverflow.com/q/12021578/1816580). I've updated my answer. – Artjom B. Oct 29 '15 at 13:05

1 Answers1

0

page.open() is asynchronous. If you want to do something on the loaded page, then you need to put that code into the page.open() callback:

page.onConsoleMessage = function(msg){
    console.log("remote> " + msg);
};

page.open("https://10.84.163.146/login/login", function(status){
    console.log(page.content);

    page.render('Bild2.png');
    console.log(page.content);
    page.evaluate(function(){
        console.log(document.getElementsByClassName("form-group input-group auth"));
        console.log(document.getElementById("user_account"));
        document.getElementsByClassName("form-control auth")[0].value="op";
    });
});

getElementsByClassName("form-control auth") returns a NodeList which doesn't have a value property. You need to get an element first.

Since, you're loading an HTTPS resource and using PhantomJS 1.x, you should use additional commandline arguments when running PhantomJS:

phantomjs --ignore-ssl-errors=true --ssl-protocol=any script.js

See PhantomJS failing to open HTTPS site for more information.

Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • but how can it find anything if it doesnt seem to load the page correctly (the page.content output is empty and the picture is blank) – Helyx Oct 27 '15 at 13:41
  • if i execute the script with your modification there is no output in the console at all – Helyx Oct 27 '15 at 13:46