0

I want to click a html object based on title

<a class="class_id" style="background: url(&quot;http://cdn.server.net/Img/openid/openid-logos.png?v=8&quot;) repeat scroll -1px -518px rgb(255, 255, 255);" href="javascript:openid.signin('google');" title="log in with Google"></a>

but my code doesn't work

require 'rubygems'
require 'mechanize'

agent = Mechanize.new
page = agent.get('http://server.com/')
page = page.link_with(:text=>'log in').click
page = page.link_with(:title=>'log in with Google').click

it returns (eval):14:in 'block (2 levels) in links_with': undefined method 'title' for #<Mechanize::Page::Link:0x1f6aeb0> (NoMethodError)

is there any way how to find and click an object using title?

Radek
  • 13,813
  • 52
  • 161
  • 255

1 Answers1

1

A couple of options:

page.links.find{|l| 'log in with Google' == l.attributes[:title]}

or

page.link_with(:node => page.at('a[@title="log in with Google"]'))
pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • Both your lines return 'nil' when I do puts 'your code'.inspect. Any idea? – Radek Sep 10 '12 at 11:13
  • Some stray whitespace perhaps? – pguardiario Sep 10 '12 at 11:43
  • The html code is copied from the source so it should work, right? – Radek Sep 10 '12 at 11:45
  • 1
    If you're copying from the inspect/firebug panel then the html might not really be there in the response. You need to check for it in page.body – pguardiario Sep 11 '12 at 00:03
  • Didn't know that. Do you mean from view source code or from whatever I capture by mechanize? Actually what I do is I get a page by mechanize, save it, open in a browser, find what I need to and add next step to my code. – Radek Sep 11 '12 at 23:26
  • In that case it should be there, I'm not sure why you're not locating it. – pguardiario Sep 11 '12 at 23:44