首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在CasperJS中使用输入id登录表单?

如何在CasperJS中使用输入id登录表单?
EN

Stack Overflow用户
提问于 2014-05-13 17:45:55
回答 2查看 1K关注 0票数 0

input元素没有name属性,所以我必须使用id of input元素。我最初使用的代码是:

代码语言:javascript
复制
casper.start('https://mp.weixin.qq.com/', function() { 
    this.fillSelectors('form#login-form', { 
        'input[id="account"]':  usr, 
        'input[id="password"]': passwd 
    }, true); 
});

就是不管用所以我试过fillXPath()

代码语言:javascript
复制
casper.start('https://mp.weixin.qq.com/', function() { 
    this.fillXPath('form#login-form', { 
        '//input[@id="account"]':  usr, 
        '//input[@id="password"]': passwd 
    }, true); 
});

也不能工作。仅供参考:

代码语言:javascript
复制
 # phantomjs --version
 1.9.7
 # casperjs --version
 1.1.0-beta3

运行报告:

代码语言:javascript
复制
root@do1:/var/www/casperjs-phantomjs# casperjs test.js aaaaa@qq.com mysecret
[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: https://mp.weixin.qq.com/, HTTP GET
[debug] [phantom] Navigation requested: url=https://mp.weixin.qq.com/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://mp.weixin.qq.com/"
[debug] [phantom] Automatically injected ./jquery-1.11.1.min.js client side
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/3 https://mp.weixin.qq.com/ (HTTP 200)
[info] [remote] attempting to fetch form element from selector: 'form#login-form'
[debug] [remote] Set "null" field value to aaaaa@qq.com
[debug] [remote] Set "null" field value to *******
[info] [remote] submitting form to unknown, HTTP GET
[info] [phantom] Step anonymous 2/3: done in 970ms.
[debug] [phantom] Navigation requested: url=https://mp.weixin.qq.com/?, type=FormSubmitted, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://mp.weixin.qq.com/?"
[debug] [phantom] Automatically injected ./jquery-1.11.1.min.js client side
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 3/3 https://mp.weixin.qq.com/? (HTTP 200)
Page url is https://mp.weixin.qq.com/?
Page title is 公众平台登录
[info] [phantom] Step anonymous 3/3: done in 1532ms.
[info] [phantom] Done 3 steps in 1549ms

登录页面为:https://mp.weixin.qq.com/

EN

回答 2

Stack Overflow用户

发布于 2014-05-14 05:09:13

fillSelectors应该可以正常工作,但您可以使用sendKeys和作为解决方法:

代码语言:javascript
复制
casper.start('https://mp.weixin.qq.com/', function() {
    this.sendKeys('input[id="account"]', "my_account");
    this.sendKeys('input[id="password"]', "my_password");
    this.click('input[type="submit"]');
});
票数 0
EN

Stack Overflow用户

发布于 2014-11-08 19:42:23

将函数选择器用于casper.fillSelectors http://www.w3.org/TR/css3-selectors/#id-selectors函数(‘https://mp.weixin.qq.com/’,css3 (){

代码语言:javascript
复制
    this.fillSelectors('form#login-form', { 
        '#account':  usr, 
        '#password': passwd 
    }, true); 
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23627875

复制
相关文章

相似问题

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