Wait for the item to have specific text with CasperJS

I use CasperJS to read a specific web page. I want to do a webpage loading in CasperJS. Then wait until the specific HTML element has certain text.

So what I would be interested to do:

var casper = require('casper').create(); casper.start('http://www.example.com/somepage', function() { this.echo('Home page opened'); }); // wait for text based on a CSS selector casper.waitForText('.someCssClass', 'dolor sit', function() { this.echo('found title!'); }); // when text is eventually found, then continue with this casper.then(function() { ... } ); casper.run(); 

So I would like to use waitForText , but with a CSS selector. So that he can track a piece of text in an HTML certaim element. For me, this is not entirely obvious if and how it is possible.

Can this be done in CasperJS? If so, how can I do this?

+5
source share
1 answer

The following function takes some logic from the waitForText() function and connects it to waitForSelector() :

 var utils = require("utils"); casper.waitForSelectorText = function(selector, text, then, onTimeout, timeout){ this.waitForSelector(selector, function _then(){ this.waitFor(function _check(){ var content = this.fetchText(selector); if (utils.isRegExp(text)) { return text.test(content); } return content.indexOf(text) !== -1; }, then, onTimeout, timeout); }, onTimeout, timeout); return this; }; 

Put this code somewhere in the begging of your script and use the function like any other CasperJS function. text can be a string or regular expression, and the selector can also be an XPath expression (using a helper function).

+6
source

All Articles