Get elements by attributes
i will be short.
As far as I know, the watir library provides two methods for getting html elements.
For almost every element (div, button, table, li, etc.) watir provides two methods:
... One is the "one" method, which only gets one specific item. For example:
watir_instance.div(:id,'my_div_id')
watir_instance.link(:href,'my_link_href')
watir_instance.button(:class =>'my_button_class', :index => 4)
These methods will only retrieve one item. This is normal...
... The second is the "plural" method that will retrieve ALL elements of the watir instance
watir_instance.divs
watir_instance.links
watir_instance.buttons
But as far as I know, watir does not provide a method to get multiple items giving certain conditions.
For example ... If I want to tag all links with id: my_link_id it would be very easy to do something like this:
watir_instance.divs(:id, 'my_link_id').each do |link|
link.flash
end
With hpricot, this task is very easy ... but if your goal is not to parse, I couldn't find a Watir method that does what I want.
I hope you can understand me ...
Cheers, Juan !!
source to share
Juan,
your script has several problems:
- You say you want to fold all links, but then you use
watir_instance.divs
. It should bewatir_instance.links
- you are passing arguments to the
divs
: methodwatir_instance.divs(:id, 'my_link_id')
. It should be easywatir_instance.divs
Your example is weird as well:
I want to fold all links with ID: my_link_id
As far as I know, the ID must be unique on the page.
So, here are some examples:
1) Flash all links on this page:
require "watir"
b = Watir::IE.start "http://stackoverflow.com/questions/1434697"
b.links.each do |link|
link.flash
end
2) Flash all links on this page that have questions
in the URL (bonus: scroll down the page so you can see the flashing link):
require "watir"
b = Watir::IE.start "http://stackoverflow.com/questions/1434697"
b.links.each do |link|
if link.href =~ /questions/
link.document.scrollintoview
link.flash
end
end
source to share