A simple CasperJS script to periodically check for a link and process accordingly.
/*jshint strict:false*/
/*global CasperError console phantom require*/
var casper = require('casper').create({
logLevel: "debug",
verbose: true
}),
interval = 1000 * 60, // 1 minute
url = 'http://google.com/'; // URL to check
casper.checkLink = function checkLink() {
this.echo('Setting up check steps...');
this.open(url).then(function() {
if (this.exists('#selector-to-your-link')) {
this.echo('link was found, clicking');
this.thenClick('#selector-to-your-link').then(function() {
// add stuff to be done
// ... then exit
this.echo('Done.').exit();
});
} else {
this.warn('Link not found, scheduling another attempt');
this.wait(interval);
}
});
this.run(this.checkLink);
};
casper.start().checkLink();
Sample output:
$ casperjs waiter.js
[info] [phantom] Starting...
Setting up check steps...
[debug] [phantom] opening url: http://google.com/, HTTP GET
[debug] [phantom] Navigation requested: url=http://google.com/, type=Other, lock=true, isMainFrame=true
[info] [phantom] Running suite: 1 step
[debug] [phantom] Navigation requested: url=http://www.google.com/, type=Other, lock=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=http://www.google.fr/, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "http://www.google.fr/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step 1/1 http://www.google.fr/ (HTTP 301)
[warning] [phantom] Link not found, scheduling another attempt
⚠ Link not found, scheduling another attempt
[info] [phantom] Step 1/1: done in 1265ms.
[info] [phantom] Step 2/2 http://www.google.fr/ (HTTP 301)
[info] [phantom] Step 2/2: done in 1364ms.
[info] [phantom] wait() finished waiting for 10000ms.
[info] [phantom] Done 2 steps in 11387ms
Setting up check steps...
[debug] [phantom] opening url: http://google.com/, HTTP GET
[debug] [phantom] Navigation requested: url=http://google.com/, type=Other, lock=true, isMainFrame=true
[info] [phantom] Running suite: 3 steps
[debug] [phantom] Navigation requested: url=http://www.google.com/, type=Other, lock=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=http://www.google.fr/, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "http://www.google.fr/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step 3/3 http://www.google.fr/ (HTTP 301)
[warning] [phantom] Link not found, scheduling another attempt
⚠ Link not found, scheduling another attempt
[info] [phantom] Step 3/3: done in 11896ms.
[info] [phantom] Step 4/4 http://www.google.fr/ (HTTP 301)
[info] [phantom] Step 4/4: done in 11992ms.
[info] [phantom] wait() finished waiting for 10000ms.
[info] [phantom] Done 4 steps in 22015ms
Setting up check steps...
[debug] [phantom] opening url: http://google.com/, HTTP GET
[debug] [phantom] Navigation requested: url=http://google.com/, type=Other, lock=true, isMainFrame=true
[info] [phantom] Running suite: 5 steps
[debug] [phantom] Navigation requested: url=http://www.google.com/, type=Other, lock=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=http://www.google.fr/, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "http://www.google.fr/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step 5/5 http://www.google.fr/ (HTTP 301)
[warning] [phantom] Link not found, scheduling another attempt
⚠ Link not found, scheduling another attempt
[info] [phantom] Step 5/5: done in 22525ms.
[info] [phantom] Step 6/6 http://www.google.fr/ (HTTP 301)
[info] [phantom] Step 6/6: done in 22622ms.
^C