title: Using Ruby and Selenium Webdriver find_elements to select and click a specific link based on criteria
body: <p>I am very new to Ruby and Selenium. </p>
  
  <p>I am trying to write a script that will open a web page (not a page I run), look through the list of items there, and click on the "Details" link for the item that meets certain criteria. A very stripped-down version of the page is:</p>
  
  <pre><code>&lt;div class="list"&gt;
  
  &lt;div class="item"&gt;
      &lt;div class="description"&gt;Cat&lt;/div&gt;
      &lt;div class="price"&gt;$3.00&lt;/div&gt;
      &lt;div class="detailslink"&gt;
          &lt;a href="http://a.htm"&gt;Details&lt;/a&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="item"&gt;
      &lt;div class="description"&gt;Dog&lt;/div&gt;
      &lt;div class="price"&gt;$4.00&lt;/div&gt;
      &lt;div class="detailslink"&gt;
          &lt;a href="http://b.htm"&gt;Details&lt;/a&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="item"&gt;
      &lt;div class="description"&gt;Cat&lt;/div&gt;
      &lt;div class="price"&gt;$4.00&lt;/div&gt;
      &lt;div class="detailslink"&gt;
          &lt;a href="http://c.htm"&gt;Details&lt;/a&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="item"&gt;
      &lt;div class="description"&gt;Bird&lt;/div&gt;
      &lt;div class="price"&gt;$3.00&lt;/div&gt;
      &lt;div class="detailslink"&gt;
          &lt;a href="http://d.htm"&gt;Details&lt;/a&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>An example of what I would like to do is click on the "Details" link for the most expensive animal that is NOT a dog. I am guessing that I would create an array of all the "item" class elements with find_elements that don't include the word "dog", find the index of highest price within that array, and click on the link in the corresponding "detailslink", but I do not know how to write that out in Ruby. </p>
  
  <p>Ideally it would also refresh every 30 seconds if there were no list items that met the criteria (there were no "item" divs within the "list" div, or all of the "list" divs contained Cat). Here is what I have so far (I know it is missing a lot!):</p>
  
  <pre><code>require "selenium-webdriver"
  browser = Selenium::WebDriver.for :chrome
  browser.get "http://list.htm"
  for i in 0..1
      items = browser.find_elements(:class=&gt;"item")
      #Do testing here. If there are non-cats, get the index of the max.
          break
      end
      sleep(30)
      browser.get "http://list.htm"
      redo
  end
  #find the nth element based on the test above
  browser.find_element(:class, "detailslink")[index].click
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>