XPath Tester For Selenium

XPath Tester (For Selenium)


Used to test a particular XPath you have in mind, by returning all
the matches, including all of their descendants.

The software is a simple .tar.gz file
with just the code itself, the code for my XPath Finder For Selenium,
and the HTML I used for the examples below.

Requirements

  • Perl
  • XML::LibXML
    • which requires libxml2
  • You’ll likely need Tidy as well
  • it’s only been tested on Linux

How To Use It


In terms of understanding what it’s for, it’s really easiest to just
show you a session using it.

First, get your Selenium script to the point where you’re trying to
find the particular element you have in mind, and then dump the HTML
source of the page in that state. In Perl this is something like:

my $source = $sel->get_html_source();
open(my $fh, '>', "output.html") or die $!;
print $fh $source;


Then, unless it’s already xhtml, turn it into proper XML with tidy:

tidy -asxhtml output.html >output.xhtml


Let’s say, artificially, that I have an item that’s easily
identifiable and takes me to a place more-or-less hierachically
adjacent to what I want, but not the right place. In the example in
the tarball, I’m looking for the second entry like

<a target="_blank" href="http://jbotcan.org/jbo/res/1313.html">RE: lojbo pixra (stela se ckiku)</a>


and using

<a target="_blank" href="http://identi.ca/timojbor">lazni - Identi.ca</a>


to make sure I’m in the right place on the page, so:

$ ./xpath_tester '//a[text()="lazni - Identi.ca"]' example.html     


--------------------------------------------------------------------------

<a target="_blank" href="http://identi.ca/lazni">lazni - Identi.ca</a>

--------------------------------------------------------------------------


Great, now step up to get to where we can see the whole set of ul elements that we want:

rlpowell@chain> ./xpath_tester '//a[text()="lazni - Identi.ca"]/..' example.html          


--------------------------------------------------------------------------

<li class="rssitem">
                    <a target="_blank" href="http://identi.ca/lazni">lazni - Identi.ca</a>


                                </li>

--------------------------------------------------------------------------


Skipping a few more rounds of that we get:

rlpowell@chain> ./xpath_tester '//a[text()="lazni - Identi.ca"]/../../../..' example.html


--------------------------------------------------------------------------

<div class="cbox-data">
<ul class="rsslist"><div style="list-style:square inside url(http://jbotcan.org/favicon.ico)">
                        <li class="rssitem">
                    <a target="_blank" href="http://jbotcan.org/jbo/res/1313.html">RE: lojbo pixra (stela se ckiku)</a>


                                </li>
                        </div>
 
[snip]


Then we drill back down:

rlpowell@chain> ./xpath_tester '//a[text()="lazni - Identi.ca"]/../../../../ul[1]/div[2]' example.html


--------------------------------------------------------------------------

<div style="list-style:square inside url(http://jbotcan.org/favicon.ico)">
                        <li class="rssitem">
                    <a target="_blank" href="http://jbotcan.org/jbo/res/1313.html">RE: lojbo pixra (stela se ckiku)</a>


                                </li>
                        </div>

--------------------------------------------------------------------------


A couple more additions and we’ve got an exact element:

rlpowell@chain> ./xpath_tester '//a[text()="lazni - Identi.ca"]/../../../../ul[1]/div[2]/li/a' example.html


--------------------------------------------------------------------------

<a target="_blank" href="http://jbotcan.org/jbo/res/1313.html">RE: lojbo pixra (stela se ckiku)</a>

--------------------------------------------------------------------------


Total elapsed time: maybe 5 minutes. A lot better than having to
re-run a full Selenium script for each test change.