ruby - Pick the correct form from Mechanize results via CSS selector -
so use mechanize fetch list of forms page. form identification can vary websites, need more stable, such selector.
is there way can use simple css selector pick right form list of forms provided mechanize?
yes. page object returned mechanize nokogiri document can use of nokogiri's methods find node in dom.
search
, at
generic methods, , both take either css or xpath selectors. at
equivalent search('some selector').first
. use 2 vast majority of times need find node. there css
, xpath
selector-type specific , at
equivalents of at_css
, at_xpath
.
consider code:
require 'mechanize' agent = mechanize.new page = agent.get('http://www.example.net') page.class # => mechanize::page page.at('title').class # => nokogiri::xml::element page.at('title').text # => "example domain"
this example gets page's <title>
can interpolate how particular form dom you're working this.
Comments
Post a Comment