4

This is a follow-up to the Counting the number of HTML elements having same attribute in Watir question.

So, suppose I have a HTML element as follows

<input type="password" class="foo" /> 
<span class="foo"></span>
<a href='1' class="foo">Text</a>

So, I can obtain a collection of all the elements having the same class name by using

 elements = browser.elements(:class,"foo")

Since, its an collection, I can use the each method to iterate over the collection. While iterating over the collection, I want to determine what type of tag is represented? (Something similar to the nodeName or the tagName method in Javascript). Is there a way we could do this in Watir?

Sample code would be :

elements = browser.elements(:class,"foo")
elements.each { |element|
puts element.<Watir_method_similar_to_nodeName_of_JavaScript>
}
Community
  • 1
  • 1
chaitanya
  • 1,591
  • 2
  • 24
  • 39

1 Answers1

3
elements.each {|element| puts element.tag_name}

Output:

input
span
a
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125
  • Isn't this method present in the watir gem since I was getting the NoMethodError when I ran it in irb. After googling, I found that this method is present in the watir-webdriver gem. Doesn't the watir gem currently support a similar method? – chaitanya Jun 29 '11 at 14:09
  • If you got the error, then it does not support it. :) I use watir-webdriver almost all the time. – Željko Filipin Jun 29 '11 at 14:22
  • The time to move to watir-webdriver is now. :) – Željko Filipin Jun 30 '11 at 09:09