anahorn

Selenium WebDriver Ruby: Part 7 (Select Boxes) 2

Jun 4th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.78 KB | None | 0 0
  1. require 'rubygems'
  2. require 'selenium-webdriver'
  3.  
  4. browser = Selenium::WebDriver.for :firefox
  5. browser.get "http://localhost/page7"
  6.  
  7. wait = Selenium::WebDriver::Wait.new(:timeout => 15)
  8.  
  9. # Interact with the drop down box
  10. select_list = wait.until {
  11.     element = browser.find_element(:name, "dropdown")
  12.     element if element.displayed?
  13. }
  14. select_list.clear
  15.  
  16. # Extract all options from the select box
  17. options=select_list.find_elements(:tag_name => "option")
  18.  
  19. # Select the option "Volvo"
  20. options.each do |g|
  21.   if g.text == "Volvo"
  22.   g.click
  23.   break
  24.   end
  25. end
  26.  
  27. # Select another option "Audi"
  28. options.each do |g|
  29.   if g.text == "Audi"
  30.   g.click
  31.   break
  32.   end
  33. end
  34.  
  35. # Print currently selected options
  36. options.each do |g|
  37.   if g.selected?
  38.   puts g.text
  39.   end
  40. end
  41.  
  42. browser.quit
Advertisement
Add Comment
Please, Sign In to add comment