Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check element using casperjs

I am trying to use casperjs to check if the following element exists in the DOM:

<b>Bar</b>

Is there a simple way to do that using selectors and casper.exists? The casperjs documentation makes it very unclear.

like image 542
JRR Avatar asked Mar 22 '23 21:03

JRR


1 Answers

The basic use of casper.exists() is to pass it a selector string and it'll return true or false to indicate whether any elements were found to match that selector.

In your case, however, the only practical selector that would find this element is b, so if you're specifically looking for a b element containing this text then you'll need to either specify some form of context (e.g. a parent element), or test this element's existence using XPath instead, which lets you test elements by their text content:

var x = require('casper').selectXPath;

if (casper.exists(x('//b[text()="Bar"]'))) {
    // <b>Bar</b> exists
}
like image 107
BoltClock Avatar answered Mar 24 '23 11:03

BoltClock