2
votes

I want to select value from drop down box using Ruby with watir-webdriver. Here is the command

browser.select_list(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").select("Whiskey")

and i got an error unable to locate element, using {:id=>"ctl00_SampleContent_ComboBox1_ComboBox1_OptionList", :tag_name=>"select"}

Any ideas what is wrong? Here is full code:

# 1.Open http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Default.aspx
  #browser = Watir::Browser.new
  #browser = Watir::Browser.new :ie
  profile = Selenium::WebDriver::Firefox::Profile.from_name 'WatirWebDriver'
  #profile.add_extension 'autoauth-2.1-fx+fn.xpi'
  browser = Watir::Browser.new :firefox, :profile => profile
  browser.goto 'http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Default.aspx'

  #2.Click ComboBox link on the left pane of the page
  browser.a(:id, 'ctl00_SamplesLinks_ctl15_SamplesLink').click

  #3.Verify that http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx URL opened
  if browser.url.eql? "http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx"
    puts "Error loading page \"http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx URL opened\""
    return false
  end

  #4.Select “Whiskey” in the combo-box
  #browser.select_list(:id, 'ctl00_SampleContent_ComboBox1_ComboBox1_OptionList').select_value('Whiskey')
  puts "!!!"
  browser.select_list(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").when_present.select("Whiskey")
1
Can you also paste in the HTML for the element that Watir-webdriver can't locate? - Abe Heward
If I follow the links you use to get to the demo combobox page the select list that comes up for me has an id of: "ctl00_SampleContent_ComboBox1_ComboBox1_TextBox" ... and a type of "text" ... That would explain why your code's not working, but perhaps I'm somehow not getting to the same page as you, which is why I'm asking for you to provide a sample of the HTML you're trying to interact with. - Abe Heward
Here is code without other choices: <ul id="ctl00_SampleContent_ComboBox1_ComboBox1_OptionList" class="ajax__combobox_itemlist" style="visibility: visible; z-index: 1000; overflow-x: hidden; overflow-y: auto; width: 187px; position: absolute; height: 288px; left: 354px; top: 191px; display: block;"> ... <li style="background-color: highlight; color: highlighttext;">Whiskey</li> ... </ul> - Sergii
Abe Heward you understood all right: I need to go to that site and choose "Whiskey" and check whether i've chosen "Whiskey" - Sergii
Željko's is the right answer. - Abe Heward

1 Answers

4
votes

This does the job:

require "watir-webdriver"
browser = Watir::Browser.new
browser.goto "http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ComboBox/ComboBox.aspx"
browser.button(id: "ctl00_SampleContent_ComboBox1_ComboBox1_Button").click
browser.ul(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").li(text: "Whiskey").click