http://www.perlmonks.org?node_id=846372

CColin has asked for the wisdom of the Perl Monks concerning the following question:

Hi - have started using Selenium with Perl - trying to find out how to locate text then turn it into the sort of XPath style link that Selenium wants. So, I can get to the page I want, which has a bunch of dynamic text with loosely associated links (so not hyperlinks, so the  ("link=$whatever") syntax for the click method is no use, in a table I want to click.
Anyway, since the starting point is static, I can get to the page of links I want. I discovered this method which looks like it may be useful from this point:
my $html = $sel->get_html_source();

So I can pull HTML, and therefore locate the text in the html - but how to turn this into the Xpath style links that Selenium expects?
By XPath style links I am referring to the output that you get from using the Selenium IDE - interacting with the website I can see that this produces clickable output like this:
$xpath = qq(//table[\@id='table_12345']/tbody/tr[4]/td[7])
If I can get to the point of working out that the HTML can be converted to this by parsing said HTML, then all will be fine, as using $sel->click($xpath) works fine.
Using HTML::Treebuilder::XPath looked like it might be the right thing to do, but it seems you already have to know how to query your document using XPath. Seems to me that if I knew exactly how to get the XPath syntax to click the link I wouldn't need it in the first place... all I have to go on is the text to start with.
Thanks for any pointers.

Replies are listed 'Best First'.
Re: Links with Selenium in XPath
by Fletch (Bishop) on Jun 24, 2010 at 18:50 UTC

    If your problem is not knowing how to write XPath expressions then perhaps something like the Selector Gadget bookmarklet would help? It lets you click on a bookmark to load an overlay which lets you click on a rendered page and retrieve the jQuery-CSS-y selector or an XPath expression that matches the selected elements (which you then could use in your perl).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Thanks - though I didn't use this, my problem was indeed not understanding XPath. Turns out to be easier than I thought, my problem was thinking it was more than what it is - just a means of querying some XML/ HTML.