0

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?

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • It's a good idea to make sure the sample HTML you post is valid because that can make a big difference in how the target nodes are accessed. What you gave in the first example isn't valid. – the Tin Man Jan 22 '13 at 19:34
  • @theTinMan Yes,I just tried to give here format,that I have. as the full html is too big. – Arup Rakshit Jan 22 '13 at 19:36
  • Then you strip it down to the essentials needed to replicate the problem, while keeping it valid. – the Tin Man Jan 22 '13 at 19:37
  • @theTinMan from your experience I would expect that you can give me a better soluntion in this [post](http://stackoverflow.com/questions/14454058/getting-error-getaddrinfo-no-such-host-is-known-socke-terror-with-mechaniz/14454623#comment20132420_14454623). may I? – Arup Rakshit Jan 22 '13 at 19:46

2 Answers2

2

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"
Guilherme Bernal
  • 8,183
  • 25
  • 43
2

You could try iterating over all of the links on the page and checking if the text is the same.

doc.xpath('//a[@href]').each do |link|
  if link.text.strip == "bla bla"
    # Your code here.
end
sunnyrjuneja
  • 6,033
  • 2
  • 32
  • 51