javascript - Get every element with a specific (data-) attribute in CasperJS -
i'm trying test page using casperjs. there possibilities every element (paragraphs) contains attribute data-...? after that, need compare attribute inner html, don't know how it.
first need retrieve representations of of elements you're interested in. can done casper.getelementsinfo(selector)
:
var elements = casper.getelementsinfo("p[data-whatever]");
this give <p>
elements on page have data-whatever
attribute set. if want filter values of data-whatever
attributes, need use attribute selectors according needs.
the getelementsinfo()
function contains useful information, doesn't contain actual elements want use. contains representation array.
you can iterate on element representations , run operations on them. said want "compare attribute inner html". can done in way:
elements.foreach(function(element){ if (element.attributes["data-whatever"] === element.html) { casper.echo("data attribute , content equal"); } else { casper.echo("data attribute , content different"); } });
keep in mind can on elements directly normal dom functions, have inside of casper.evaluate()
, because phantomjs (which casperjs built upon) has 2 contexts , page context sandboxed. also, cannot return dom nodes page context.
Comments
Post a Comment