0

I didn't think that find_elements would return an array with elements and nil. Look at what I'm looking at in IRB.

irb(main):025:0>  w = $driver.find_elements(:tag_name, 'tr')
=> [#<Selenium::WebDriver::Element:0x77dbee53bb7dc76a id="{2cea6188-1aa3-9045-acbc-c4e7e6b789ee}">, #<Selenium::WebDriver::Element:0x1000c5ad74eacaee id="{79c30952-9be5-3e4e-be0d-1f8c527541ff}">]
irb(main):026:0> w[1]
=> #<Selenium::WebDriver::Element:0x1000c5ad74eacaee id="{79c30952-9be5-3e4e-be0d-1f8c527541ff}">
irb(main):027:0> w[2]
=> nil

What is going on? Bug in Selenium or bug in Ruby?
ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13] selenium-webdriver 2.39.0

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
Zach
  • 885
  • 2
  • 8
  • 27

1 Answers1

2

w = $driver.find_elements(:tag_name, 'tr') gives you an Array.

In Ruby Array indexing starts at 0, as in C or Java. A negative index is assumed to be relative to the end of the array---that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.

So in your case w is a sized 2 array. w[0] will give you first and w[1] will second element, and there is no element at index 2,3,4 and so on.. So every attempt to those index will give you nil element.

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317