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 ();
Run:
Casperjs Baidu.js
Get output:
"Baidu a bit, you know"
A second simple CASPERJS code
The execution script for Casperjs is a concatenation of one step. Start represents the first step, then the subsequent step is then represented by then, followed by:
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 ();
When we're done, we'll get a title on the console, and we'll get the picture captured in then Baidu-homepage.jpg
A third simple CASPERJS code
We're trying to get Casperjs to complete the 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 a 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 website
ThoughtWorks written Zz_thoughtworks Recruitment experience
About Us | ThoughtWorks
ThoughtWorks ranked first in the interview of the most difficult technology companies _ Baidu Library
Transparent album-thoughtworks XI ' an office
Stevok Software Technology (XI ' an) Co., Ltd. ThoughtWorks software ...
Places to be aware of:
1) Create Casper, we inject jquery, this jquery must be stored locally, and HTTP access is not valid.
2) this.evaluate (gettitles) can be understood, we in Casperjs, the GetTitles this method is injected into the visited page, in the visited page to execute this method and ask its return value.
3) access page log gets: 2) The gettitles is actually executed in the visited page, if we add a section of Console.log code in GetTitles, how can we get the console information of the page being visited?
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 for the Console.log event of the page being accessed, rather than exporting to Casperjs
Reference: Http://docs.casperjs.org/en/latest/faq.html#can-i-use-jquery-with-casperjs