如何使用Selenium :: WebDriver :: Element#click从SELECT元素中选择一个选项?

时间:2011-06-29 14:51:52

标签: selenium capybara

使用capybara 1.0.0和selenium-webdriver 0.2.0并在测试中我可以从下拉列表中选择以下内容。

select 'Food & Dining', :from => 'category_id'

测试通过,但我得到以下投诉:

Selenium::WebDriver::Element#select is deprecated. Please use Selenium::WebDriver::Element#click ...

我在网上搜索过,文档稀疏,有人知道如何使用click来选择select元素的选项吗?

3 个答案:

答案 0 :(得分:10)

从capybara-1.0.0来源:

# File 'lib/capybara/node/actions.rb', line 110
def select(value, options={})
  if options.has_key?(:from)
    no_select_msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
    no_option_msg = "cannot select option, no option with text '#{value}' in select box '#{options[:from]}'"
    select = find(:xpath, XPath::HTML.select(options[:from]), :message => no_select_msg)
    select.find(:xpath, XPath::HTML.option(value), :message => no_option_msg).select_option
  else
    no_option_msg = "cannot select option, no option with text '#{value}'"
    find(:xpath, XPath::HTML.option(value), :message => no_option_msg).select_option
  end
end

从查看生成警告的selenium-webdriver 0.2.2代码:

  # File 'rb/lib/selenium/webdriver/common/element.rb', line 175
  # Select this element
  #

  def select
    warn "#{self.class}#select is deprecated. Please use #{self.class}#click and determine the current state with #{self.class}#selected?"

    unless displayed?
      raise Error::ElementNotDisplayedError, "you may not select an element that is not displayed"
    end

    unless enabled?
      raise Error::InvalidElementStateError, "cannot select a disabled element"
    end

    unless selectable?
      raise Error::InvalidElementStateError, "you may only select options, radios or checkboxes"
    end

    click unless selected?
  end

所以作为烦人消息的临时解决方案,直到capybara人员将其修复到最后,我将这段代码添加到我的cucumber features / support / env.rb文件中,你也可以将它添加到spec_helper.rb或者在运行测试之前加载的任何测试框架文件。 基本上我打开课程并覆盖方法选择并禁用警告...只是一个临时的黑客...不为此感到自豪......

# June 30th, 2011
# a temporary hack to disable the annoying upstream warnings capybara > selenium-webdriver 0.2.2
# capybara folks know about this and are working on it. See:
# http://groups.google.com/group/ruby-capybara/browse_thread/thread/2cd042848332537a/7edb1699cb314862?show_docid=7edb1699cb314862
# Remove this whole block when Capybara 1.0.1 or greater are used
module Selenium
  module WebDriver
    class Element
      #
      # Select this element
      #

      def select
        #warn "#{self.class}#select is deprecated. Please use #{self.class}#click and determine the current state with #{self.class}#selected?"

        unless displayed?
          raise Error::ElementNotDisplayedError, "you may not select an element that is not displayed"
        end

        unless enabled?
          raise Error::InvalidElementStateError, "cannot select a disabled element"
        end

        unless selectable?
          raise Error::InvalidElementStateError, "you may only select options, radios or checkboxes"
        end

        click unless selected?
      end
    end
  end
end

答案 1 :(得分:4)

对于那些仍然遇到此问题的人,您可以简单地使用主分支中的最新资本,这应该解决所有问题,包括Launchy的最新问题。

只需改变:

gem 'capybara'

gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'

然后

bundle update

你很好^ _ ^

答案 2 :(得分:1)

您也可以将以下内容添加到Gemfile

gem 'selenium-webdriver', '0.2.1'