Search code examples
rubymechanizemechanize-ruby

Can I get HREF values using Mechanize which are not within the form?


I have similar HTML in one of my pages (with form name):

<form name="test">
<td> <A HREF="http://www.edu/st/file.html">bla bla</A> </td>
<td> <A HREF="http://www.mac/spgm/file.html">boo bla</A> </td>
<td> <A HREF="http://www.dom/st/file.html">foo</A> </td>
</form>

Another page without form name:

<td> <A HREF="http://www.edu/st/file.html">bla bla</A> </td>
<td> <A HREF="http://www.mac/spgm/file.html">boo bla</A> </td>
<td> <A HREF="http://www.dom/st/file.html">foo</A> </td>

In both cases I have the values bla bla, boo, foo. Using the values, can I get the respective href values using Mechanize?


Solution

  • You can just take the href attribute from the link:

    m = Mechanize.new
    page = m.get("yourpage")
    page.link_with(:text => "bla bla").href #=> "http://www.edu/st/file.html"