Ruby - Mechanize: select a link by title and other questions

I'm currently looking at Mechanize. I am new to Ruby, so please be patient.

I wrote a small test script:

require 'rubygems' require 'mechanize' agent = WWW::Mechanize.new page = agent.get('http://www.google.de') pp page.title google_form = page.form_with(:name => 'f') google_form.q = 'test' page = agent.submit(google_form) pp page.title page_links = Array.new page.links.each do |ll| page_links << ll end puts page_links.size 

It works. But page_links includes more than just search results. It also includes google links such as Login, Pictures, ... As a result, the links have a class “1” style. Is it possible to select only links with class == 1? How to achieve this?

Is it possible to change the "agration"? If I have a website, including Google analytics or something like that, which browser will I see in ga coming with mechanization on my website?

Can I select elements by their identifier instead of name? I tried to use

 my_form = page.form_with(:id => 'myformid') 

But that will not work.

+4
source share
3 answers

You can create a list of links to the search result by changing the code as follows:

 page.links.each do |ll| cls = ll.attributes.attributes['class'] page_links << ll if cls && cls.value == 'l' end 

For each ll element in page.links , ll.attributes is Nokogiri::XML::Element , and ll.attributes.attributes is a Hash containing the attributes in the link, so ll.attributes.attributes need to get the actual class and need to check for zero before comparing the value with 'l'

The problem with using :id in the criteria for finding the form is that it encounters the Ruby Object#id method to return the internal identifier of the Ruby object. I'm not sure what this is working for. You can easily choose a form with another attribute (for example, its action.)

+2
source

in cases like me, I use the Nokogiri DOM search. Here is your code rewritten a bit:

 require 'rubygems' require 'mechanize' agent = Mechanize.new page = agent.get('http://www.google.de') pp page.title google_form = page.form_with(:name => 'f') google_form.q = 'test' page = agent.submit(google_form) pp page.title page_links = Array.new #maybe you better use 'h3.r > al' here page.parser.css("al").each do |ll| #page.parser here is Nokogiri::HTML::Document page_links << ll puts ll.text + "=>" + ll["href"] end puts page_links.size 

This article is probably a good place to start: getting-started-with-nokogiri By the way, the samples in the article also relate to Google search;)

+4
source

I believe that the selector you are looking for is:
:dom_id . in your case:
my_form = page.form_with(:dom_id => 'myformid')

0
source

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


All Articles