Oleksandr Holubenko
Oleksandr Holubenko

Reputation: 4440

How to simulate click in span using Mechanize Ruby?

I have a webpage with list of pages:

<div class="pager">    
  <span class="current_page">1</span>
  <span class="page" samo:page="2">2</span>
  <span class="page" samo:page="3">3</span>
  <span class="page" samo:page="4">4</span>
  <span class="page" samo:page="5">5</span>
  <span class="page" samo:page="6">6</span>
  <span class="page" samo:page="7">7</span>
  <span class="page" samo:page="8">8</span>
  <span class="page" samo:page="9">9</span>
  <span class="page" samo:page="10">10</span>
  <span class="page" samo:page="11">11</span>
</div>

How can I click on the span using mechanize?

Upvotes: 0

Views: 1036

Answers (1)

Jorge de los Santos
Jorge de los Santos

Reputation: 4633

According to this ASCIIcasts you can perform searches and findings:

There are two methods on the page object that we can use to extract elements from a page using Nokogiri. The first of these is called at and will return a single element that matches a selector.

agent.page.at(".edit_item")  

The second method is search. This is similar, but returns an array of all of the elements that match.

agent.page.search(".edit_item")

http://asciicasts.com/episodes/191-mechanize

So doing something like:

agent.page.at(".page")

Will return the array of spans. And then you will be able to work with them and just do the #click action.

EDITED:

As long as the span is a non interactive element, and click is a Link action, you will have to find a workaround:

How to click link in Mechanize and Nokogiri?

Upvotes: 1

Related Questions