【发布时间】:2011-01-28 23:21:45
【问题描述】:
我尝试使用 mechanize (Ruby) 访问表单。 在我的表单上,我有一组单选按钮。 所以我想检查其中之一。
我写道:
target_form = (page/:form).find{ |elem| elem['id'] == 'formid'}
target_form.radiobutton_with(:name => "radiobuttonname")[2].check
在这一行中,我想检查值为 2 的单选按钮。 但在这一行,我得到一个错误:
: undefined method `radiobutton_with' for #<Nokogiri::XML::Element:0x9b86ea> (NoMethodError)
【问题讨论】:
-
根据文档,您的表单应该是
Mechanize::Form类,而不是Nokogiri::XML::Element。你是怎么得到target_form的? -
target_form: target_form = (page/:form).find { |elem| elem['id'] == 'formid'}
-
您应该使用
Mechanize::Page#form_with方法来获取表单。顺便说一句,我根本不确定您的页面是Mechanize::Page。 -
page = agent.get('mypage.xy') 这就是我定义页面的方式
-
我想你有多个同名的单选按钮,你需要第三个。在这种情况下,只需调用
radiobuttons_with而不是radiobutton_with方法(注意复数)。