如何使用Ruby获取Selenium Webdriver中所有下拉选项的文本?

时间:2013-07-18 10:11:48

标签: ruby selenium webdriver selenium-webdriver

2 个答案:

答案 0 :(得分:1)

尝试以下操作。它在我的工作结束:

require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox

driver.get "http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/checkboxes/defaultcs.aspx"

el = driver.find_element(:id, "ctl00_ContentPlaceHolder1_RadComboBox1_Arrow")
el.click

list = driver.find_element(:xpath, "//ul[@class='rcbList']")
options = list.find_elements(:xpath, ".//li/label")
options.each do |option|
    puts option.text
end

答案 1 :(得分:0)

无论是否使用Selenium::WebDriver::Wait,我都可以使用。

这是完整的代码(我没有使用WebDriverWait注释掉版本,两个版本都应该可以使用):

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get('http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/checkboxes/defaultcs.aspx')

wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds

# without wait
# combo = driver.find_element(:id => 'ctl00_ContentPlaceHolder1_RadComboBox1_Input')
# combo.click

# with wait
combo = wait.until { driver.find_element(:id => "ctl00_ContentPlaceHolder1_RadComboBox1_Input") }
combo.click

# without wait
# all_labels = driver.find_elements(:css => '#ctl00_ContentPlaceHolder1_RadComboBox1_DropDown .rcbItem label')

# with wait
all_labels = wait.until { driver.find_elements(:css => '#ctl00_ContentPlaceHolder1_RadComboBox1_DropDown .rcbItem label') }
all_labels.each do |label|
  puts label.text
end
puts all_labels.count