简体   繁体   中英

Watir/Ruby selecting next value

I am working with a table that has links in the first column:

html = Nokogiri::HTML(browser.html)

html.css('tr td a').each do |links|
  browser.link(:text=>"#{a}").click
  puts "#{a}"
end

How do i display the NEXT value for the link? If the link name is abcd but the next one is efgh, how do i get it to write the efgh?

You should be able to achieve this using the index in the array you are working with.

thing = ['a', 'b', 'c', 'd']

(0..thing.length - 1).each do |index| 
  puts thing[index + 1]
end

I don't understand the use case here (not at all), but this contrived example might point you in the direction that you're looking to go.

Use the links method to create an array of link objects. Then, you can print the text for the element at the second position but click the element at the first position.

require 'watir-webdriver'
b = Watir::Browser.new 
b.goto('http://www.iana.org/domains/reserved')

nav_links = b.div(:class => "navigation").links   

puts nav_links[1].text   #=> NUMBERS
nav_links[0].click
puts b.url               #=> http://www.iana.org/domains

The Enumerable::each_with_index method might also be useful since it cycles through each element of an array and additionally returns the respective element position. For example:

b.div(:class => "navigation").links.each_with_index { |el, i| puts el.text, i }

#=> DOMAINS
#=> 0
#=> NUMBERS
#=> 1
#=> PROTOCOLS
#=> 2
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM