首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Casper js :无法使用casper js登录网站

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

Stack Overflow用户
提问于 2018-02-19 15:40:28
回答 1查看 80关注 0票数 0

我正在抓取一个网站使用PHP,我能够登录到网站和抓取网站的数据。现在我已经切换到Casper js,但它不允许我登录站点。也尝试使用不同的用户代理和IP,但不能获得任何成功。

代码语言:javascript
复制
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();
});

任何建议都将是有帮助的,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-02-20 22:38:04

首先,我建议使用这个来启动你的Casper:

代码语言:javascript
复制
casper = require('casper').create({
  verbose: true,
  logLevel: 'debug',
  viewportSize: {width: 1280, height: 800},
});

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

为了回答你的问题,在login.tmall.com,登录是在iframe中,所以你需要首先切换到那个iframe来填写表单。你可以用这个实现这一点。

代码语言:javascript
复制
casper.then(function() {
  this.withFrame(0, function() {
    this.fill('form#J_Form', {
        'TPL_password':    '123',
        'TPL_username':    'xyz',
    }, true);
  });
});

然后将其与您的代码相结合,您将得到以下代码

代码语言:javascript
复制
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();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48861458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档