如何根据标题查找并单击对象?

时间:2012-09-09 01:26:36

标签: ruby mechanize mechanize-ruby

我想点击基于标题的html对象

<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>

但我的代码不起作用

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

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

有没有办法如何使用标题查找和点击对象?

1 个答案:

答案 0 :(得分:1)

有两种选择:

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

page.link_with(:node => page.at('a[@title="log in with Google"]'))