Phantom.js .NET身份验证

时间:2014-01-31 15:26:34

标签: .net authentication web-scraping web-crawler phantomjs

尝试针对asp.net网站验证phantom.js时有哪些常见问题?

这是我的具体问题;我能够导航到asp.net网站并适当填写登录表单,我通过渲染一个简单的test.png文件来验证这一点。到目前为止,一切都按预期工作。

我提交表单后(form.submit()element.click();),页面会重新加载,但不会重定向到网站的身份验证方。它与验证失败的页面完全相同。但不同之处在于,新呈现的页面已从密码字段中删除了密码。

我知道凭据是正确的,因为我可以从任何浏览器登录。

我目前正在使用以下测试脚本,我从另一篇文章中收到并进行了一些小的更改。有人可以帮助我或指出我正确的方向吗?

var page = require('webpage').create(),
    testindex = 0,
    loadInProgress = false;

page.onConsoleMessage = function(msg) {
  console.log(msg);
};

page.onLoadStarted = function() {
  loadInProgress = true;
  console.log("load started");
};

page.onLoadFinished = function() {
  loadInProgress = false;
  console.log("load finished");
};

/*
page.onNavigationRequested = function(url, type, willNavigate, main) {
  console.log('Trying to navigate to: ' + url);
  console.log('Caused by: ' + type);
  console.log('Will actually navigate: ' + willNavigate);
  console.log('Sent from the page\'s main frame: ' + main);
};
*/


console.log('The default user agent is ' + page.settings.userAgent);
//changing the user agent to IE just in case!
page.settings.userAgent = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/4.0; InfoPath.2; SV1; .NET CLR 2.0.50727; WOW64)';
console.log('The new user agent is ' + page.settings.userAgent);

/*
  The steps array represents a finite set of steps in order to perform the unit test
*/

var steps = [
  function() {
    //Load Login Page
    page.open("https://www.aspxpage/signin.aspx");
  },
  function() {
    //Enter Credentials
    page.evaluate(function() {

      //Fill the form with the correct credintals
      //var loginForm = document.getElementById("aspnetForm");
      document.querySelector('input[name=id]').value = 'blah';
      document.querySelector('input[name=user]').value = 'blah';
      document.querySelector('input[name=password]').value = 'blah';



    });
  }, 
  function() {
    //Login
    page.evaluate(function() {
    //Submit the form 
    //form submit

    //1.
        //var loginForm = document.getElementById("formid");
        //loginForm.submit();

    //2.
    //button click
        var button = document.querySelector('input[id=buttonid]');
    button.click();
        //console.log(button);


    });
  }, 
  function() {
    // Output content of page to stdout after form has been submitted
    page.evaluate(function() {
      //console.log(document.querySelectorAll('html')[0].outerHTML);
    });

    //render a test image to see if login passed
    page.render('test.png');

  }
];


interval = setInterval(function() {
  if (!loadInProgress && typeof steps[testindex] === "function") {
    console.log("step " + (testindex + 1));
    steps[testindex]();
    testindex++;
  }
  if (typeof steps[testindex] !== "function") {
    console.log("test complete!");
    phantom.exit();
  }
}, 50); 

1 个答案:

答案 0 :(得分:1)

我认为50毫秒时间不够,所以你可以进入登录后页面。您应该链接最后一个呼叫,这样您就不会干扰登录过程。而且,为了坚固/健全,我建议你不要在密码填充和按钮点击之间进行渲染(这可能会触发一些页面刷新并从文本框中删除密码)。保持渲染代码暂时用于调试目的 - 但不是用于测试整个调用链。