使用casperjs设置页面和表单

问题描述 投票:0回答:1

您好我需要使用浏览器自动化来扫描我的网站中的结果信息。我有这个脚本:

var casper = require('casper')。create(); console.log(“casper create OK”);

casper.start(“https://portale.spefin.it/anagraph/legalperson/atc”,function(){console.log(“Connexion URL OK”);

// set a wait condition to make sure the page is loaded (particularly iframe in my case)

    //fill out the form 
    this.fillSelectors("form[name='login']",{
        'input#username' : "XXXXXXXXX",
        'input#pw' : "XXXXXXXX"
    });
    console.log("Renseignement login et pass OK");

    // click the login button 
    this.click("button[type='submit']");
    console.log("Passage bouton login OK");


    // switch to iframe (won't be necessary for most)
    this.page.switchToChildFrame('https://portale.spefin.it/anagraph/legalperson/atc');
    console.log("Switch page OK");

    this.wait(5000,function(){
console.log("Attente 5 sec OK");

    this.fillSelectors("form[name='advancedFilterForm']",{
       'input#tax_code' : "11057560150"


    });
    console.log("partita iva ok!");

        // Test d'une zone sur la page pour valider la connexion
        //casper.waitForSelector('.area-status', function() {
        //console.log("Validation element sur la page OK");
        //});
}); 

});

问题是页面没有设置,表格没有找到....请帮帮我!

casperjs
1个回答
0
投票

首先,我建议使用verboselogLevel进行测试,如下所示:

var casper = require('casper').create({
    verbose: true,
    logLevel: "debug"
});

您也可以更频繁地使用then承诺。相信我,它有助于一堆。

使用waitForSelector进行救援。

Bellow找到不同版本的代码。

casper.start("https://portale.spefin.it/anagraph/legalperson/atc").then(function(){
    this.waitForSelector("form[name='login']", function(){
        /*form found*/

        //the [true] parameter will submit the form, no need for the button click
        this.fillSelectors("form[name='login']",{
            'input#username' : "XXXXXXXXX",
            'input#pw' : "XXXXXXXX"
        }, true);

        this.then(function(){
            //possibly no need for the wait but...
            //if you really want to use it
            this.wait(5000,function(){
               //In my honest opinion you don't need the [switchToChildFrame]
               //but instead use [waitForSelector]

               this.waitForSelector("form[name='advancedFilterForm']", function(){
                   /*form found*/

                   //the [true] parameter will submit the form
                   this.fillSelectors("form[name='advancedFilterForm']",{
                       'input#tax_code' : "11057560150"
                   }, true);

                   this.then(function(){
                       this.waitForSelector(".area-status", function(){
                           /*element found*/
                           //do what you must perhaps get the info

                           require('utils').dump(this.getElementInfo('.area-status'));

                       }, function(){
                           /*element not found*/
                       })
                   });

               },function(){
                   /*could not find advancedFilterForm*/
               })
            });
        });

    },function(){
        /*form not found*/
    });
});

注意:此代码未经过测试。

希望能帮助到你。 :)

好的报废

© www.soinside.com 2019 - 2024. All rights reserved.