【问题标题】:How does one use Selenium::WebDriver::Element#click to select an option from a SELECT element?如何使用 Selenium::WebDriver::Element#click 从 SELECT 元素中选择一个选项?
【发布时间】:2011-09-25 05:53:04
【问题描述】:

使用 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 ...

我在网上搜索过,文档很少,有人知道如何使用点击来选择选择元素的选项吗?

【问题讨论】:

    标签: selenium capybara


    【解决方案1】:

    从 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
    

    因此,作为对烦人消息的临时解决方案,直到水豚最终修复它,我将此代码块添加到我的黄瓜 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
    

    【讨论】:

      【解决方案2】:

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

      只是改变:

      gem 'capybara'
      

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

      然后

      bundle update
      

      你很好^_^

      【讨论】:

      • 并非如此。这样做了吗?现在我在测试中得到了“未定义的方法click' for class Capybara::Driver::RackTest::Node”
      • 这个答案可能有点过时了,这个问题@simianarmy
      【解决方案3】:

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

      gem 'selenium-webdriver', '0.2.1'
      

      【讨论】:

      • -1 遗憾的是,这不适用于 capybara 1.0.0,它无法连接到 firefox。我试过 0.2.1 和 0.2.0,但只有 0.2.2 有效。
      猜你喜欢
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      相关资源
      最近更新 更多