簡體   English   中英

如何在CasperJS中使用輸入ID登錄表單?

[英]How to login in the form using input id in CasperJS?

input元素沒有name屬性,因此我必須使用input元素的id 最初,我使用以下代碼:

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

只是行不通,所以我嘗試了fillXPath()

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

也行不通。 僅供參考:

 # phantomjs --version
 1.9.7
 # casperjs --version
 1.1.0-beta3

運行報告:

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/ : https://mp.weixin.qq.com/

fillSelectors應該可以正常工作,但是您可以使用sendKeys單擊作為一種解決方法:

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"]');
});

為casper.fillSelectors使用css3選擇器http://www.w3.org/TR/css3-selectors/#id-selectors casper.start(' https://mp.weixin.qq.com/',function (){

    this.fillSelectors('form#login-form', { 
        '#account':  usr, 
        '#password': passwd 
    }, true); 
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM