Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Links with Selenium in XPath

by CColin (Scribe)
on Jun 24, 2010 at 18:38 UTC ( #846372=perlquestion: print w/replies, xml ) Need Help??
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 (Chancellor) 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://846372]
Approved by Marshall
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2018-01-22 21:26 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (237 votes). Check out past polls.