Casper js:无法使用casper js登录网站

时间:2018-02-19 07:40:28

标签: casperjs

我正在使用PHP抓取网站,我也可以登录网站并抓取网站数据。 现在我已切换到Casper js,但它不允许我登录网站。 试图使用不同的用户代理和IP,但无法取得任何成功。

casper = require('casper').create();
casper.on('started', function () {
  this.page.customHeaders = {
    "User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0", 
    "Accept": "*/*", 
    "Accept-Language": "en-US,en;q=0.8",
  }
});
casper.start('https:somesite.com');

casper.then(function() {
    this.fill('form#J_Form', {
        'TPL_password':    '123',
        'TPL_username':    'xyz',
    }, true);
});
casper.wait(20000, function() {
    this.echo("I've waited for a 2 seconds.");
});
 casper.then(function() {
    casper.capture('Screeenshots/loginsuccessfully1.png');
  });
 casper.thenOpen('https://item.othersite.com/item.htm?id=538450584178', function() {
 this.echo(this.getHTML());
 });
casper.run(function() {
    this.echo('login successfully').exit();
});

任何建议都会有所帮助 感谢。

1 个答案:

答案 0 :(得分:0)

首先我建议你用它来启动你的casper

casper = require('casper').create({
  verbose: true,
  logLevel: 'debug',
  viewportSize: {width: 1280, height: 800},
});

因为它将为您提供有关该过程中实际发生情况的更多信息。

要回答您的问题,请在login.tmall.com上登录iframe,因此您需要先切换到该iframe以填写表单。你可以用这个来做到这一点。

casper.then(function() {
  this.withFrame(0, function() {
    this.fill('form#J_Form', {
        'TPL_password':    '123',
        'TPL_username':    'xyz',
    }, true);
  });
});

然后将此与您的代码相结合,您就拥有了这个

casper = require('casper').create({
  verbose: true,
  logLevel: 'debug',
  viewportSize: {width: 1280, height: 800},
});
casper.on('started', function () {
  this.page.customHeaders = {
    "User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0", 
    "Accept": "*/*", 
    "Accept-Language": "en-US,en;q=0.8",
  }
});
casper.start('https://login.tmall.com/', function() {
  this.capture('test.png');
});

casper.then(function() {
  this.withFrame(0, function() {
    this.fill('form#J_Form', {
        'TPL_password':    '123',
        'TPL_username':    'xyz',
    }, true);
  });
});
casper.wait(20000, function() {
    this.echo("I've waited for a 2 seconds.");
});
 casper.then(function() {
    casper.capture('Screeenshots/loginsuccessfully1.png');
  });
 casper.thenOpen('https://item.othersite.com/item.htm?id=538450584178', function() {
 this.echo(this.getHTML());
 });
casper.run(function() {
    this.echo('login successfully').exit();
});

希望有所帮助