.. _selectors: .. index:: selector, DOM, HTML ========= Selectors ========= CasperJS makes a heavy use of selectors in order to work with the `DOM `_, and can transparently use either `CSS3 `_ or `XPath `_ expressions. All the examples below are based on this HTML code: .. code-block:: html My page


  • one
  • two
  • three

©2012 myself

.. index:: CSS, CSS3 CSS3 ---- By default, CasperJS accepts `CSS3 selector strings `_ to check for elements within the DOM. To check if the ``

`` element exists in the example page, you can use:: var casper = require('casper').create(); casper.start('http://domain.tld/page.html', function() { if (this.exists('h1.page-title')) { this.echo('the heading exists'); } }); casper.run(); Or if you're using the :doc:`testing framework `:: casper.test.begin('The heading exists', 1, function suite(test) { casper.start('http://domain.tld/page.html', function() { test.assertExists('h1.page-title'); }).run(function() { test.done(); }); }); Some other convenient testing methods are relying on selectors:: casper.test.begin('Page content tests', 3, function suite(test) { casper.start('http://domain.tld/page.html', function() { test.assertExists('h1.page-title'); test.assertSelectorHasText('h1.page-title', 'Hello'); test.assertVisible('footer'); }).run(function() { test.done(); }); }); .. index:: XPath XPath ----- .. versionadded:: 0.6.8 You can alternatively use `XPath expressions `_ instead:: casper.start('http://domain.tld/page.html', function() { this.test.assertExists({ type: 'xpath', path: '//*[@class="plop"]' }, 'the element exists'); }); To ease the use and reading of XPath expressions, a ``selectXPath`` helper is available from the ``casper`` module:: var x = require('casper').selectXPath; casper.start('http://domain.tld/page.html', function() { this.test.assertExists(x('//*[@id="plop"]'), 'the element exists'); }); .. warning:: The only limitation of XPath use in CasperJS is in the :ref:`casper.fill() ` method when you want to fill **file fields**; PhantomJS natively only allows the use of CSS3 selectors in its `uploadFile method `_, hence this limitation.