Casperjs is a PHANTOMJS-based tool that makes it easier to navigation than PHANTOMJS.
One of the simplest CASPERJS codes
Create a file baidu.js. To simulate our visit to Baidu page
var casper = require (' Casper '). Create ();
Casper.start (' http://www.baidu.com/', function () {
This.echo (This.gettitle ());
});
Casper.run ();
Perform:
Casperjs Baidu.js
Get output:
"Baidu a bit, you know"
A second simple CASPERJS code
The run script of the Casperjs is made up of one step in series. Start represents the first step. Then the subsequent step is then represented by then and then run in turn:
var casper = require (' Casper '). Create ();
Casper. Start (' http://www.baidu.com/', function () {
This.echo (This.gettitle ());
});
Casper. Then (function () {
This.capture (' baidu-homepage.png ');
})
Casper.run ();
After the completion. We'll get a title on the console, and at the same time we'll get the picture captured in then Baidu-homepage.jpg
A third simple CASPERJS code
We'll find a way to get Casperjs finished. Search function:
var casper = require (' Casper '). Create ();
Casper.start (' http://www.baidu.com/', function () {
This.echo (This.gettitle ());
});
Casper.then (function () {
This.capture (' baidu-homepage.png ');
});
Casper.then (function () {
This.fill (' form[action= '/S "] ', {wd: ' ThoughtWorks '}, True);//fill in the form. To search
});
Casper.then (function () {
This.capture (' thoughtworks-search-results.png ');
});
Casper.run ();
Get the Thoughtworks-search-results.png:
How to introduce jquery
In some cases, jquery is convenient.
var casper = require (' Casper '). Create ({
Clientscripts: ["Jquery.js"]
});
Casper.start (' http://www.baidu.com/', function () {
This.echo (This.gettitle ());
});
Casper.then (function () {
This.fill (' form[action= '/S "] ', {wd: ' ThoughtWorks '}, True);
});
Casper.then (function () {
Search_result_titles =this.evaluate (gettitles)
This.echo (Search_result_titles.join (' \ n '))
});
function GetTitles () {
var titles = $.map ($ ("h3.t a"), function (link) {
return $ (link). Text ()
});
Return titles
}
Casper.run ();
return Result:
Thoughtworks_ Baidu Encyclopedia
Chengdu thoughtworks-Recruitment Specialist--Location: Chengdu Job Information | ThoughtWorks recruitment ...
Agile Development and Experiential design | ThoughtWorks
ThoughtWorks Basic situation and treatment "Coward Savior _ Workplace Ancient Boxing Law"
An encounter with ThoughtWorks (i)-Redhat-iteye technology site
ThoughtWorks written Zz_thoughtworks Recruitment experience
About Us | ThoughtWorks
ThoughtWorks ranked first in the interview of the most difficult technology companies _ Baidu Library
Transparent albums-thoughtworks XI ' an office
Stevok Software Technology (XI ' an) Co., Ltd. ThoughtWorks software ...
Places to note:
1) When create Casper, we inject jquery. This jquery must be stored locally and is not valid via HTTP access.
2) this.evaluate (gettitles) can be understood as. In Casperjs, we injected this approach into the GetTitles page. Run this method on the access page and ask for its return value.
3) Access to the page log gets: 2) in the GetTitles is actually in the interview page run, assuming we add a section of Console.log code in GetTitles, how can we get access to the page console information?
Casper.then (function () {
This.page.onConsoleMessage = function (e) {
Console.log (e);
}
Search_result_titles = This.evaluate (gettitles)
This.echo (Search_result_titles.join (' \ n '))
})
This allows you to listen to the Console.log event of the visited page, rather than exporting to Casperjs
References: Http://docs.casperjs.org/en/latest/faq.html#can-i-use-jquery-with-casperjs
Take the initiative to browse the page with Casperjs