I am very new to Ruby and Selenium.
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:
<div class="list">
<div class="item">
<div class="description">Cat</div>
<div class="price">$3.00</div>
<div class="detailslink">
<a href="http://a.htm">Details</a>
</div>
</div>
<div class="item">
<div class="description">Dog</div>
<div class="price">$4.00</div>
<div class="detailslink">
<a href="http://b.htm">Details</a>
</div>
</div>
<div class="item">
<div class="description">Cat</div>
<div class="price">$4.00</div>
<div class="detailslink">
<a href="http://c.htm">Details</a>
</div>
</div>
<div class="item">
<div class="description">Bird</div>
<div class="price">$3.00</div>
<div class="detailslink">
<a href="http://d.htm">Details</a>
</div>
</div>
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.
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!):
require "selenium-webdriver"
browser = Selenium::WebDriver.for :chrome
browser.get "http://list.htm"
for i in 0..1
items = browser.find_elements(:class=>"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
Any help would be greatly appreciated!