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 :
PS The first line above is achieved simply by target_form = page.at_css('#formid') .
source share