anahorn

Selenium WebDriver Ruby: Part 8 (Tables) 2

Jun 4th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.09 KB | None | 0 0
  1. require 'rubygems'
  2. require 'selenium-webdriver'
  3.  
  4. browser = Selenium::WebDriver.for :firefox
  5. browser.get "http://localhost/page8"
  6.  
  7. wait = Selenium::WebDriver::Wait.new(:timeout => 15)
  8.  
  9. # Check that the table with the given id is displayed
  10. table = wait.until {
  11.     element = browser.find_element(:id, "booktable")
  12.     element if element.displayed?
  13. }
  14. puts "Test Passed: Book Table found" if table
  15.  
  16. # Print full table contents
  17. puts "Table with id=booktable:\n" + table.text
  18.  
  19. # Iterate through all cells of the table
  20. browser.find_elements(:xpath => "//table[@id='booktable']/tbody/tr/td").each do |r|
  21.     puts "Cell Value: " + r.text
  22. end
  23.  
  24. # Print the value from the 1'st column of the 2'nd row of the table
  25. puts "Value from the 1'st column of the 2'nd row of the table: " + browser.find_elements(:xpath => "//table[@id='booktable']/tbody/tr")[1].text.split(' ')[0]
  26.  
  27. # Print the value from the 2'nd column of the 1'st row of the table
  28. puts "Value from the 2'nd column of the 1'st row of the table: " + browser.find_elements(:xpath => "//table[@id='booktable']/tbody/tr")[0].text.split(' ')[1]
  29.  
  30. browser.quit
Advertisement
Add Comment
Please, Sign In to add comment