1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Finding Frames and IFrames


While Mechanize doesn't automatically load frames and iframes, you can still search for the elements and "click" on them to visit that URL. While it doesn't make much sense to "click" on an iframe or frame, this has to do with how frames are implemented by Mechanize, the Mechanize::Page::Frame is a child class of Mechanize::Page::Link. In other words, Mechanize treats them like links, and to visit the URL for the frame or iframe, "click" on it.

As frames and iframes are links, finding them is much like finding links. In fact, even though frames use slightly different terminology than links, you can still use the link terminology. For example, links have an href attribute, and frames have a src attribute, but you can use either href or src to look for frames.

The following snipped loads up http://www.reddit.com and looks for the ad iframe. It then "clicks" on the iframe to visit that URL.

agent = Mechanize.new
page = agent.get('http://www.reddit.com/')

iframe = page.iframe_with(:id => 'ad-frame')
page2 = iframe.click
  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. The Mechanize 2.0 Handbook
  6. Finding Frames and IFrames

©2014 About.com. All rights reserved.