# Clicking link with WWW::Mechanize

**URL:** <https://www.ruby-forum.com/t/clicking-link-with-www-mechanize/90399>\
**Category:** Ruby\
**Created:** [March 28, 2007, 12:44pm UTC](https://www.ruby-forum.com/t/clicking-link-with-www-mechanize/90399 "2007-03-28T12:44:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [March 28, 2007, 12:44pm UTC](https://www.ruby-forum.com/t/clicking-link-with-www-mechanize/90399/1 "2007-03-28T12:44:50Z")

</div>

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](http://www.rubyrailways.com) :: Ruby and Web2.0 blog  
[http://scrubyt.org](http://scrubyt.org) :: Ruby web scraping framework  
[http://rubykitchensink.ca/](http://rubykitchensink.ca/) :: The indexed archive of all things Ruby.

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [March 28, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/clicking-link-with-www-mechanize/90399/2 "2007-03-28T21:58:25Z")

</div>

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/](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](http://rubyforge.org/mailman/listinfo/mechanize-users)

Sure, I am going to subscribe right now!

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

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [March 28, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/clicking-link-with-www-mechanize/90399/3 "2007-03-28T17:52:24Z")

</div>

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/](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](http://rubyforge.org/mailman/listinfo/mechanize-users)

Hope that helps!
