Mechanize links_with is not filtering the text properly

175 views Asked by At

I am trying to click a list of links with Mechanize gem, but apparently Mechanize's links_with(criteria) is not properly filtering based on the criteria. For debugging purposes, I am only printing out the link.

The following script is printing out most (all?) links on the page:

require 'mechanize'

agent = Mechanize.new
url = "http://www.fearlessphotographers.com/location/470/sul-do-brasil"

agent.get(url)
agent.page.links_with(:text => /[VIEW FULL PROFILE]/).each do |link|
    puts link.text
end

And if I change the (:text => /[VIEW FULL PROFILE]/) to (:text => "VIEW FULL PROFILE") then no link at all gets printed.

I can't understand what I am doing wrong. Any thoughts?

1

There are 1 answers

1
pguardiario On BEST ANSWER

Brackets [] have special meaning in regex. You need to escape them with a slashie /\[\]/.

On second thought, there's no brackets in those links so leave them out.

page.links_with :text => /View Full Profile/

Also notice that the text seems to be getting uppercase()'d with css.