Jak zalogować się na stronę z CasperJS?

Jak mogę zalogować się z CasperJS poprzez wysłanie formularza. Szukałem w google i nie znalazłem żadnych dobrych przykładów na ten temat.

2 answers

Będziesz musiał użyć funkcji Casper fill () .

Poniżej znajduje się przykład logowania do Facebook i wydrukować swoje imię po zalogowaniu. Pamiętaj, że musisz podać swoją nazwę użytkownika i hasło:

var casper = require('casper').create({   
    verbose: true, 
    logLevel: 'debug',
    pageSettings: {
         loadImages:  false,         // The WebPage instance used by Casper will
         loadPlugins: false,         // use these settings
         userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
    }
});

// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
});

// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
    this.echo("Page Error: " + msg, "ERROR");
});

var url = 'http://www.facebook.com/';

casper.start(url, function() {
   console.log("page loaded");
   this.test.assertExists('form#login_form', 'form is found');
   this.fill('form#login_form', { 
        email: '**<put your email here>**', 
        pass:  '**<put your password here>**'
    }, true);
});

casper.thenEvaluate(function(){
   console.log("Page Title " + document.title);
   console.log("Your name is " + document.querySelector('.headerTinymanName').textContent ); 
});

casper.run();
 52
Author: Ngo Hung,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-14 10:40:45

Oto nieco zmieniona wersja kodu z Ngo Hung. Selektor nazwy użytkownika był wyłączony, podobnie jak przypisanie userAgent:

var casper = require('casper').create({   
    verbose: true, 
    logLevel: 'debug',
    userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22',
    pageSettings: {
      loadImages:  false,         // The WebPage instance used by Casper will
      loadPlugins: false         // use these settings
    }
});

// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
});

// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
    this.echo("Page Error: " + msg, "ERROR");
});

var url = 'http://www.facebook.com/';

casper.start(url, function() {
    console.log("page loaded");
    this.test.assertExists('form#login_form', 'form is found');
    this.fill('form#login_form', { 
        email: '**<put your email here>**', 
        pass:  '**<put your password here>**'
    }, true);
});

casper.thenEvaluate(function(){
    console.log("Page Title " + document.title);
    console.log("Your name is " + document.querySelector(".fbxWelcomeBoxName").innerHTML);
});

casper.run();

Aktualizacja: polecam każdemu innemu korzystanie z własnej strony, a przynajmniej nie facebook.com jeśli planują duże ilości testów. Facebook mógł powiedzieć, że próbowałem się zalogować w kółko zaczął wysyłać mi e-maile i zmuszać mnie do potwierdzenia mojego konta za pomocą SMS-a.

 7
Author: Caleb G,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-14 11:41:09