Настройка страницы и формы с помощью casperjs - PullRequest
1 голос
/ 16 апреля 2019

Здравствуйте, мне нужно очистить информацию о результатах на моем сайте с помощью автоматизации браузера. У меня есть этот скрипт:

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

}); * * 1 010

Проблема в том, что страница не установлена, а форма не найдена ...., пожалуйста, помогите мне!

1 Ответ

0 голосов
/ 19 апреля 2019

Прежде всего, я бы порекомендовал использовать verbose и logLevel для тестирования следующим образом:

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

Также вы можете использовать обещание then чаще. Поверьте мне, это помогает куче .

Использование waitForSelector для спасения.

Ниже вы можете найти другую версию вашего кода.

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*/
    });
});

Примечание: этот код не был проверен.

Надеюсь, это поможет.:)

Хорошие выскабливания

...