1. Computing

Finding Frames and IFrames

By

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

©2014 About.com. All rights reserved.