문제

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?

도움이 되었습니까?

해결책

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"

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top