gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:35:04 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com