How can I get Mechanize objects from the Mechanize :: Page search method?

I am trying to clean a site where I can only rely on classes and a hierarchy of elements to find the right nodes. But using Mechanize::Page#search returns Nokogiri::XML::Element , which I cannot use to fill out and submit forms, etc.

I would really like to use a pure CSS selector, but matching for classes seems pretty simple with various _with methods. However, matching things like :not(.class) is pretty verbose when compared to just using CSS selectors, while I have no idea how to match the hierarchy of elements.

Is there a way to convert Nokogiri elements to Mechanize objects, or is it even better to get them directly from the search method?

+4
source share
1 answer

As pointed out in this answer , you can simply create a new Mechanize::Form object using your Nokogiri::XML::Element , obtained through Mechanize::Page#search or Mechanize::Page#at :

 a = Mechanize.new page = a.get 'https://stackoverflow.com/' # Get the search form via ID as a Nokogiri::XML::Element form = page.at '#search' # Convert it back to a Mechanize::Form object form = Mechanize::Form.new form, a, page # Use it! form.q = 'Foobar' result = form.submit 

Note. You must provide the Mechanize object and the Mechanize::Page object to the constructor so that it can submit the form. Otherwise, it will be just a Mechanize::Form object with no context.


It seems that there is no central utility function for converting Nokogiri::XML::Element to Mechanize elements, and conversions are performed where they are needed. Therefore, when writing a method that searches for a document using CSS or XPath and returns Mechanize elements, if applicable, the node type requires a fairly large commutation register. Not quite what I imagined.

+7
source

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


All Articles