Nokogiri error: undefined `radiobutton_with 'method - Why?

I am trying to access a form using mechanize (Ruby). In my form, I have a mountain of Radio Battles. So I want to check out one of them.

I wrote:

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

In this line I want to check the radio object with a value of 2. But in this line I get an error message:

 : undefined method `radiobutton_with' for #<Nokogiri::XML::Element:0x9b86ea> (NoMethodError) 
+3
source share
1 answer

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

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

Sometimes, however, you can find the item 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 cannot use the Mechanize field_with method to find a selection and get an instance of Mechanize::Form::SelectList .

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

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

In your case, when you had Nokogiri::XML::Element and you need Mechanize::Form :

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

PS The first line above is achieved simply by target_form = page.at_css('#formid') .

+5
source

Source: https://habr.com/ru/post/1394782/


All Articles