javascript - CasperJS 使用 fill() 方法登录失败

标签 javascript forms authentication fill casperjs

我是 Casper JS 新手,在使用 fill() 方法登录网站时遇到问题。 在调试输出中,我到达了登录页面的网址,该网址应该在登录后到达。然而,它然后将我发送到一个空白页面并中断:

[debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=false

我已经测试了下面的代码来登录 Facebook,它有效。我还使用下面的 Casperjs 代码的 Phantomjs 版本成功登录到下面示例中的站点。因此,该问题似乎特定于使用 Casperjs fill() 方法登录。

1) 这是 html(出于安全目的我将其匿名):

//login form
<form id="***id_form***" class="loginBox" method="post" action="https://***login_link***/">

// input login
<input id="IDToken1" type="text" value="" name="IDToken1">

// input password
<input id="IDToken2" type="password" name="IDToken2">

2)这是我的 Casperjs 代码:

var start_url = 'https://***login_url***'

casper.start(start_url, function() {

    this.test.assertExists('form#***id_form***', 'form is found');
    this.fill('form#***id_form***', {
        'IDToken1': '***MY_ID***',
        'IDToken2': '***MY_PASSWORD***'
    }, true);
});

casper.run();

3)这是控制台输出(我隐藏了网址):

[info] [phantom] Starting...

[info] [phantom] Running suite: 2 steps

[debug] [phantom] opening url: https://***login_url***, HTTP GET

[debug] [phantom] Navigation requested: url=https://***url***, type=Other, lock=true, isMainFrame=true

[debug] [phantom] Navigation requested: url=https://***url***, type=Other, lock=true, isMainFrame=true

[debug] [phantom] url changed to "https://***url***"

[debug] [phantom] Successfully injected Casper client-side utilities

[info] [phantom] Step 2/2 https://***url*** (HTTP 302)
PASS form is found

[info] [remote] attempting to fetch form element from selector: 'form#***id_form***'

[debug] [remote] Set "IDToken1" field value to ***My_ID***

[debug] [remote] Set "IDToken2" field value to ***My_PASSWORD***

[info] [remote] submitting form to https://***login_link***, HTTP POST

[info] [phantom] Step 2/2: done in 1211ms.

[debug] [phantom] Navigation requested: url=https://***login_link***, type=FormSubmitted, lock=true, isMainFrame=true

[debug] [phantom] Navigation requested: url=https://***url***, type=FormSubmitted, lock=true, isMainFrame=true

[debug] [phantom] Navigation requested: url=http://***url***, type=FormSubmitted, lock=true, isMainFrame=true

[debug] [phantom] url changed to "http://***url***"

[debug] [phantom] Successfully injected Casper client-side utilities

[debug] [phantom] Navigation requested: url=https://***url***, type=Other, lock=true, isMainFrame=true

[debug] [phantom] Navigation requested: url=https://***url***, type=Other, lock=true, isMainFrame=true

[debug] [phantom] Navigation requested: url=https://***landing_page***, type=Other, lock=true, isMainFrame=true

[debug] [phantom] url changed to "https://***landing_page***"

[debug] [phantom] Navigation requested: url=https://***url***, type=Other, lock=true, isMainFrame=false

[debug] [phantom] Navigation requested: url=about:blank, type=Other, lock=true, isMainFrame=false

感谢任何帮助。谢谢!

最佳答案

尝试以下操作:

casper.start(start_url, function() {
    this.waitForSelector('form#***id_form***', function() {
        this.fill('form#***id_form***', {
            'IDToken1': '***MY_ID***',
            'IDToken2': '***MY_PASSWORD***'
        }, true);
    }
});

casper.then(function() {
   ...
}

关于javascript - CasperJS 使用 fill() 方法登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15708358/

相关文章:

javascript - 在传单 "on()"方法中传递 JSON var

javascript - 在 recursive-readdir 中包含多个文件扩展名

c++ - GTK 3 如何将一个信号连接到多个小部件?

authentication - 为什么API网关和认证服务应该不同?

c# - 为什么我的表单例份验证票过期得这么快?

javascript - 向下滚动箭头 CSS

javascript - 如何在 webpack Angular2 中包含外部 javascript 或 css

php - 实体选择列表形式的其他

html - 如何将 'boolean'类型的数据从HTML表单发送到数据库

c# - 使用 .Net Standard 2.0 库中的 x509 证书使用 WCF Web 服务?