如何在casperjs中维护会话?

时间:2017-01-27 05:39:42

标签: casperjs

在CasperJS中,如何在使用casper.thenOpen()

时保持会话

例如:

var casper = require('casper').create();

casper.start('http://chaseonline.com/', function() {
    this.echo(this.getTitle());
    this.evaluate(function() {
       document.getElementById("userid").value = "a@b.com";
       document.getElementById("password").value = "asdf";

     });
    this.click("#btnSubmit");
});

casper.thenOpen('http://chaseonline.com/section/1/module/2/abc.jsp', function() {
  // now this page never loads because the page requires a logged in session 
  // but casperjs doesn't appear to automatically propagate the session
  this.echo(this.getTitle());
});

casper.run();

1 个答案:

答案 0 :(得分:2)

您的示例中的会话仍处于打开状态。可能你没有登录正确。
可能如果它的工作步骤(似乎我看到的网站不是你的例子中的那个,没有登录):

var casper = require('casper').create();
var x = require('casper').selectXPath;

casper.start('http://youraddess.com/', function() {
  casper.then(function() {
    casper.waitForSelector(x("xpath_selector"));
  });
  var data = {};
  casper.then(function() {
    data["//input[@id='userid']"] = "a@b.com";
    data["//input[@id='password']"] = "asdf";
    casper.fillXPath(x("//form[]"), data, false);
  });
  casper.then(function() {
    casper.click(x("//button[@id='btnSubmit']"));
  });
  casper.then(function() {
    casper.waitWhileSelector(x("xpath_selector"));
  });
});
casper.thenOpen('http://chaseonline.com/section/1/module/2/abc.jsp', function() {
  // now this page never loads because the page requires a logged in session 
  // but casperjs doesn't appear to automatically propagate the session
  casper.then(function() {
    casper.echo(this.getTitle());
    casper.capture('test.png');
  });
});

casper.run();

会话直到run()始终相同。有可能打开新的,但那很难。