This class encapsulates links. It contains the text and the URI for 'a' tags parsed out of an HTML page. If the link contains an image, the alt text will be used for that image.
For example, the text for the following links with both be 'Hello World':
<a href=“Hello”>rubyforge.org“>Hello
World</a> <a href=”rubyforge.org“>
src=”test.jpg“
alt=”Hello World“></a>
# File lib/mechanize/page/link.rb, line 20 def initialize(node, mech, page) @node = node @href = node['href'] @text = node.inner_text @page = page @mech = mech @attributes = node # If there is no text, try to find an image and use it's alt text if (@text.nil? || @text.length == 0) && node.search('img').length > 0 @text = '' node.search('img').each do |e| @text << ( e['alt'] || '') end end end
Click on this link
# File lib/mechanize/page/link.rb, line 43 def click @mech.click self end
This method is a shorthand to get link's DOM id. Common usage: page.link_with(:dom_id => “links_exact_id”)
# File lib/mechanize/page/link.rb, line 49 def dom_id node['id'] end
# File lib/mechanize/page/link.rb, line 38 def uri @href && URI.parse(WEBrick::HTTPUtils.escape(@href)) end