Tenderlove Making

More Ruby Mechanize Dev

I’ve been working away at mechanize lately. It seems like my creativity comes in bursts. As soon as I make a small bugfix release, I get inspired and write a whole bunch of stuff. Anyway, Eric pointed out that I didn’t have support for multi-select lists, so I added that for 0.5.2. I’m excited to release 0.5.3 already because I updated it so that when operating on an WWW::Mechanize::List, if the list doesn’t respond to the method being called, it will try it on the first element of the list. So, what does that mean? You can click on links without having to specify the first one all the time. You can do this: ~~~ ruby agent.click page.links.text(‘Something’) ~~~ Instead of doing this: ~~~ ruby agent.click page.links.text(‘Something’).first ~~~ But it is still an array, so if you have multiple links with the text ‘Something’, you can index in to the array like so: ~~~ ruby agent.click page.links.text(‘Something’)[2] ~~~ I’ve also been toying around with adding a “click” or “select” method to most objects. Then you can select the first radio button with either of the following lines: ~~~ ruby agent.click form.radiobuttons.first form.radiobuttons.first.click ~~~ I think this may help in the future if I want to get Javascript support. I’ve currently got this working with select lists, so this will select the second option from a dropdown: ~~~ ruby form.selectlist.options[2].select ~~~

As for WWW::Mechanize 0.6.0, I’ve got Hpricot support in the trunk with all unit tests passing. This means that browsing the web and scraping those pages at the same time will be a breeze! I just hope that Why releases a gem for Hpricot on rubyforge soon! I also want to see if I can get Mechanize working with Selenium IDE so that scripts generated by Selenium IDE can be executed using mechanize and not through the browser.

« go back