Arup Rakshit
Arup Rakshit

Reputation: 118289

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?

Upvotes: 0

Views: 1390

Answers (2)

sunnyrjuneja
sunnyrjuneja

Reputation: 6123

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

Upvotes: 2

Guilherme Bernal
Guilherme Bernal

Reputation: 8313

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"

Upvotes: 2

Related Questions