Clicking link with WWW::Mechanize

Hello,

I wuold like to click on a link in WWW::Mechaznie, and I know only the
Hpricot XPath of the element I would like to click, nothing more. While
it’s certainly possible to look up the text of the element pointed to by
the XPath, there can be more elements with that text, but only one
element with that XPath.

Is this possible with Mechanize somehow?

Thanks,
Peter

__
http://www.rubyrailways.com :: Ruby and Web2.0 blog
http://scrubyt.org :: Ruby web scraping framework
http://rubykitchensink.ca/ :: The indexed archive of all things Ruby.

Aaron P. wrote:

Is this possible with Mechanize somehow?

Yes, this is possible. Mechanize can click an hpricot element, as long
as it has an ‘href’, or a ‘src’ attribute. For example:

page = mech.get(‘http://example.com/’)
new_page = mech.click((page/‘//some/xpath/a’))

Hey, thanks Aaron! I am really glad that this is possible!

If you’ve got more questions, don’t forget about the mechanize mailing
list:

http://rubyforge.org/mailman/listinfo/mechanize-users

Sure, I am going to subscribe right now!

Cheers,
Peter
_
http://www.rubyrailways.com :: Ruby and Web2.0 blog
http://scrubyt.org :: Ruby web scraping framework
http://rubykitchensink.ca/ :: The indexed archive of all things Ruby.

Hi Peter,

On Wed, Mar 28, 2007 at 07:44:08PM +0900, Peter S. wrote:

Hello,

I wuold like to click on a link in WWW::Mechaznie, and I know only the
Hpricot XPath of the element I would like to click, nothing more. While
it’s certainly possible to look up the text of the element pointed to by
the XPath, there can be more elements with that text, but only one
element with that XPath.

Is this possible with Mechanize somehow?

Yes, this is possible. Mechanize can click an hpricot element, as long
as it has an ‘href’, or a ‘src’ attribute. For example:

page = mech.get(‘http://example.com/’)
new_page = mech.click((page/‘//some/xpath/a’))

If you’ve got more questions, don’t forget about the mechanize mailing
list:

http://rubyforge.org/mailman/listinfo/mechanize-users

Hope that helps!