views:

541

answers:

1

Hi people, i will be short.

As far as i know watir library provides two methods for getting html elements.

Almost for each element (div, button, table, li, etc) watir provides two methods:

. One is the 'singular' method which gets only one specific element. 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 A SINGLE ELEMENT. Thats ok...

. The second is the 'plural' method that will retrieve ALL the elements of the watir instance

watir_instance.divs
watir_instance.links
watir_instance.buttons

But as far as i know watir doesn't provide a method to get more than one element giving certain conditions.

For example... If i want to flash all the 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 aim is not to parse i couldn't find a Watir Method that does what i want.

Hope you can understand me...

Cheers, Juan!!

+2  A: 

Juan,

your script has several problems:

  • You say you want to flash all links, but then you use watir_instance.divs. It should be watir_instance.links
  • you pass arguments to divs method: watir_instance.divs(:id, 'my_link_id'). It should be just watir_instance.divs

Your example is also strange:

i want to flash all the links with id:my_link_id

As far as I know, id should be unique at the page.

So, here are different 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 URL (bonus: scroll the page so the link that is flashed is visible):

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
Željko Filipin
Thanks a lot Zeljko! But i think i haven't explain well my problem and the things i want to do...I was just asking if watir provides a method like divs but accepting parametres... For example if i had to flash all links with class 'my_class' i would put something like this:b = Watir::IE.start "xxxxxxx"b.links(:class,'myclass').each do |link| link.flashendDoes some method like this exist?Anyway, if this method doesn't exist, in your example i have seen the way to fix my problem; an is doing a if conditional comparing the attribute.
juanmaflyer
Short answer: no. A bit longer answer: as far as I know, when you ask for collection of elements (divs, links...), you get them all, there is no way to get just some elements (like links with specific class). Fortunately, my answer solves that problem.
Željko Filipin