Nokogiri错误:未定义方法`radiobutton_with'-为什么? [英] Nokogiri Error: undefined method `radiobutton_with' - Why?

查看:64
本文介绍了Nokogiri错误:未定义方法`radiobutton_with'-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用机械化(Ruby)访问表单. 在我的表格上,我有一小撮单选按钮. 所以我想检查其中之一.

I try to access a form using mechanize (Ruby). On my form I have a gorup of Radiobuttons. So I want to check one of them.

我写道:

target_form = (page/:form).find{ |elem| elem['id'] == 'formid'}
target_form.radiobutton_with(:name => "radiobuttonname")[2].check

在这一行中,我想检查值为2的单选按钮. 但是在这一行中,我得到一个错误:

In this line I want to check the radiobutton with the value of 2. But in this line, I get an error:

: undefined method `radiobutton_with' for #<Nokogiri::XML::Element:0x9b86ea> (NoMethodError)

推荐答案

发生此问题是因为将Mechanize页面用作Nokogiri文档(通过调用/方法,searchxpath等). )返回Nokogiri元素,而不是使用其特殊方法对元素进行机械化.

The problem occured because using a Mechanize page as a Nokogiri document (by calling the / method, or search, or xpath, etc.) returns Nokogiri elements, not Mechanize elements with their special methods.

如评论中所述,您可以通过使用 方法来查找表单.

As noted in the comments, you can be sure to get a Mechanize::Form by using the form_with method to find your form instead.

但是,有时候,您可以使用Nokogiri来找到所需的元素,而不能使用Mechanize来找到.例如,考虑一个页面上的<select>元素不在<form>内.由于没有表单,因此无法使用Mechanize field_with 方法来查找选择并获取一个Mechanize::Form::SelectList实例.

Sometimes, however, you can find the element you want with Nokogiri but not with Mechanize. For example, consider a page with a <select> element that is not inside a <form>. Since there is no form, you can't use the Mechanize field_with method to find the select and get a Mechanize::Form::SelectList instance.

如果您有Nokogiri元素并且想要Mechanize等效项,则可以通过将Nokogiri元素传递给构造函数来创建它.例如:

If you have a Nokogiri element and you want the Mechanize equivalent, you can create it by passing the Nokogiri element to the constructor. For example:

sel = Mechanize::Form::SelectList.new( page.at_xpath('//select[@name="city"]') )

在您有Nokogiri::XML::Element并想要Mechanize::Form的情况下:

In your case where you had a Nokogiri::XML::Element and wanted a Mechanize::Form:

# Find the xml element
target_form = (page/:form).find{ |elem| elem['id'] == 'formid'}
target_form = Mechanize::Form.new( target_form )

P.S. target_form = page.at_css('#formid').

这篇关于Nokogiri错误:未定义方法`radiobutton_with'-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆