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

标签 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 元素的选项吗?

最佳答案

查看 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或在运行测试之前加载的任何测试框架文件。 基本上,我正在打开类(class)并覆盖方法选择并禁用警告...只是一个临时黑客...并不为此感到自豪...

# 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

关于selenium - 如何使用 Selenium::WebDriver::Element#click 从 SELECT 元素中选择一个选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6522662/

相关文章:

python - 如何使用 FirefoxProfile 或 FirefoxOptions 通过 Selenium 设置 Firefox 浏览器的窗口位置

ruby-on-rails-3 - 使用 Poltergeist PhantomJS Capybara 驱动程序设置 Cookie

ruby - capybara 代码点击图像按钮

ruby - 无法使用 gsub 和 hash 修改卡住的字符串错误

ruby-on-rails-3 - 子域,使用 cucumber 或 capybara 和 rspec 进行测试

java - 如何在 Ubuntu MATE 中为 Raspberry Pi 2 和 Raspberry Pi 3 安装 ChromeDriver 和 Geckodriver

java - Selenium IE 驱动程序找不到新打开的窗口

java - Selenium Java - 使用 Getters 和 Setters 的 TestNG 数据提供程序

python - 如何使用 Python 将文本插入表单控件/下拉列表来解决此错误?

capybara - Rails 5 Capybara 没有建立已经点击的按钮