Search code examples
ruby-on-railsnokogirimechanize-ruby

Find and act on links based on value in a cell with Mechanize in Rails


I am trying to act on links found in a specific row in an HTML table.

The table has a column for a date and a column for city names which act as links. I need to find row with specific date and then cycle through the links found from the next cell on same row one after the another.

I reach the target page with Mechanize from the previous page (link to page is generated at previous page so direct link is not possible) and I am able to select the said table with this:

agent.page.search("//table[@class='calendar']")

But, after that, I am at a loss how to select the correct row based on the date in Date-column and click each of the links in turn (and naturally do stuff before moving on to the next city).

EDIT

I managed to make some progress. I am now able to get contents of correct cell in correct row IF I hardcode the date in. But I can't figure out how to use variable in place of hardcoded date and I also need to find a way to use just the date and not to include weekday as text in there as well.

 agent.page.search("//table[@class='calendar']/tbody/tr/td[@class='date']
    [text()='wed 27.8.2014']/following-sibling::td[1]/ul/li")

Example HTML below:

<table class="calendar">
    <tr>
        <th>Date</th>
        <th>City</th>
    </tr>
<tr class="odd">
    <td class="date">thu 28.8.2014</td>
    <td class="city">
        <ul>
            <li><a href="">London</a></li>
            <li><a href="">Paris</a></li>
            <li><a href="">Berlin</a></li>
        </ul>
    </td>
</tr>
<tr class="even">
    <td class="date">wed 27.8.2014</td>
            <td class="city">
        <ul>
            <li><a href="">London</a></li>
            <li><a href="">Paris</a></li>
            <li><a href="">Berlin</a></li>
        </ul>
    </td>
</table> 

All help is greatly appreciated!


Solution

  • With further research I was able to find the answer my self. Of great help was this question and answer Nokogiri: How to select nodes by matching text? about partial matching in Nokogiri.

    Following snippet is what works for me now and I am able to proceed.

    date = Date.new(2014,8,27)
    
    agent.page.search("//table[@class='calendar']/tbody/tr/td[@class='date']
    [contains(text(),
    \"#{date.strftime("%e.%-m.%Y")}\")]/following-sibling::td[1]/ul/li")