无法找到元素,使用{:id =>“”,:tag_name =>“select”}

时间:2013-01-24 14:07:11

标签: ruby watir-webdriver

我想使用带有watir-webdriver的Ruby从下拉框中选择值。这是命令

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

我收到了一个错误 无法找到元素,使用{:id =>“ctl00_SampleContent_ComboBox1_ComboBox1_OptionList”,:tag_name =>“select”}

任何想法有什么不对? 这是完整的代码:

# 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 个答案:

答案 0 :(得分:4)

这就是工作:

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