Ruby - click “unclickable” e.g. local link with mechanize

Please, bear in mind this is my first time writing anything to actually
achieve something more complex, also Im beginner in programming.

So first of all, this is part of my “webpage” printed with pp:


#<Mechanize::Page::Link “Date/Time” “?section=9”>
#<Mechanize::Page::Link “Voice Messages” “?section=9”>
#<Mechanize::Page::Link “LOG file” “?section=2”>
#<Mechanize::Page::Link “CDR file” “?section=7”>
#<Mechanize::Page::Link “SDR file” “?section=15”>
#<Mechanize::Page::Link “Statistics” “?section=3”>
#Mechanize::Page::Link “Messaging” “?section=151”}


And here is my code snippet:

#!/usr/bin/env ruby

require ‘mechanize’
require ‘nokogiri’

#init
url = ‘http://XXXXXXXXXX/
agent = Mechanize.new
page = agent.get(url)

#Login
form = page.form_with(:name => ‘form_login’)
form.user = ‘XXXXXXXX’
form.pass = ‘XXXXXXXX’
page = form.submit

#navigate to User Group section
test = page.links_with(:text => ‘Messaging’)

test is having this value in this stage:

{Mechanize::Page::Link '“Messaging” “?section=151”>}

What I think could work is converting test to string, extracting
“?section=int” and concating it with url variable, then navigate to that
page. BUT there must be much better way to achieve that. Is there any
way I can navigate to that subsection of the page, based on section name
(mechanize prefered)? Any help will be greatly appreciated!

You could assemble the URL if you know the pattern. There isn’t enough
information in your question to know what form this URL would take.

If you’re working on a page with JavaScript, you might need a driver
capable of handling JavaScript events, such as watir-webdriver.

You can try both approaches (URL construction and Watir-Webdriver), and
then decide which is the best one for you.

Mechanize will probably accomplish the job more quickly because it is
headless and does not execute JavaScript, but Watir-Webdriver will be
more flexible. The decision is yours, but I would recommend trying both
and deciding based on the results.

Joel P. wrote in post #1184895:

You could assemble the URL if you know the pattern. There isn’t enough
information in your question to know what form this URL would take.

If you’re working on a page with JavaScript, you might need a driver
capable of handling JavaScript events, such as watir-webdriver.

final URL should look ca. like this:

http://192.168.1.10/?section=151

I need to navigate to exact subsection and then perform some javascript
clicks etc. (thanks a bunch for mentioning watir-webdriver!). I could
extract final subsection from web page source, but what if section
number changes (and I’m 100% sure section name will stay the same)…
that’s why I want to get there step by step.

So basically you have the same opinion as I do. Extracting section part
from “test” variable with Regex and then concating/assembling it with
partial URL to get desired URL ?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs