ruby 如何使用 Capybara 在下拉列表中选择选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20134085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:18:17  来源:igfitidea点击:

How to select option in drop down using Capybara

rubyselenium-webdrivercapybara

提问by Farooq

I'm trying to select an item from a drop down menu using Capybara (2.1.0).

我正在尝试使用 Capybara (2.1.0) 从下拉菜单中选择一个项目。

I want to select by number (meaning select the second, third, etc option).

我想按数字选择(意思是选择第二个、第三个等选项)。

I've Googled like crazy trying all sorts of things but no luck.

我像疯了一样用谷歌搜索了各种各样的东西,但没有运气。

I was able to select it by using the value:

我能够通过使用值来选择它:

 find("option[value='4c430d62-f1ba-474f-8e8a-4452c55ea0a8']").click

But I don't want to use that method b/c the value is something that will change and that will make my test brittle.

但我不想使用这种方法 b/c 值是会改变的,这会使我的测试变得脆弱。

The HTML for the drop down is:

下拉菜单的 HTML 是:

<td class="value">
    <select name="organizationSelect" id="organizationSelect" class="required">
     <option value="NULL">Choose...</option>
     <option value="4c430d62-f1ba-474f-8e8a-4452c55ea0a8">&nbsp;Institution1</option>
     <option value="e1a4efa7-352d-410a-957e-35c8a3b92944">&nbsp;Institution / test</option>
    </select>
</td>

I also tried this:

我也试过这个:

  option = find(:xpath, "//*[@id='organizationSelect']/option[2]").text  
  select(option, :from => organizationSelect)

But it results in this error:

但这会导致此错误:

Ambiguous match, found 2 elements matching option "Institution" (Capybara::Ambiguous)

So how can I select the first, second, third, etc option from the drop down (using Capybara) ?

那么如何从下拉列表中选择第一个、第二个、第三个等选项(使用 Capybara)?

回答by RVM

For some reason it didn't work for me. So I had to use something else.

出于某种原因,它对我不起作用。所以我不得不使用别的东西。

select "option_name_here", :from => "organizationSelect"

worked for me.

对我来说有效。

回答by carols10cents

If you take a look at the source of the selectmethod, you can see that what it does when you pass a fromkey is essentially:

如果你看一下方法的源码select,你可以看到,当你传递一个fromkey的时候,它所做的事情本质上是:

find(:select, from, options).find(:option, value, options).select_option

In other words, it finds the <select>you're interested in, then finds the <option>within that, then calls select_optionon the <option>node.

换句话说,它找到<select>你感兴趣的,然后查找<option>内,然后调用select_option上的<option>节点。

You've already pretty much done the first two things, I'd just rearrange them. Then you can tack the select_optionmethod on the end:

你已经完成了前两件事,我只是重新排列它们。然后你可以select_option在最后添加方法:

find('#organizationSelect').find(:xpath, 'option[2]').select_option

回答by montrealmike

another option is to add a method like this

另一种选择是添加这样的方法

  def select_option(css_selector, value)
    find(:css, css_selector).find(:option, value).select_option
  end

回答by user2490003

To add yet another answer to the pile (because apparently there's so many ways of doing it depending on your setup) - I did it by selecting the literal optionelement and clicking it

添加另一个答案(因为显然根据您的设置有很多方法可以做到)-我通过选择文字option元素并单击它来做到这一点

find(".some-selector-for-dropdown option[value='1234']").select_option

It's not very pretty, but it works :/

它不是很漂亮,但它有效:/

回答by Sam D

Unfortunately, the most popular answer did not work for me entirely. I had to add .select_optionto end of the statement

不幸的是,最受欢迎的答案并不完全适合我。我不得不添加.select_option到声明的末尾

select("option_name_here", from: "organizationSelect").select_option

select("option_name_here", from: "organizationSelect").select_option

without the select_option, no select was being performed

没有select_option,没有选择被执行

回答by bjelli

none of the answers worked for me in 2017 with capybara 2.7. I got "ArgumentError: wrong number of arguments (given 2, expected 0)"

在 2017 年使用水豚 2.7 时,没有一个答案对我有用。我收到“ArgumentError:参数数量错误(给定 2,预期为 0)”

But this did:

但这确实:

find('#organizationSelect').all(:css, 'option').find { |o| o.value == 'option_name_here' }.select_option

回答by pduey

Here's the most concise way I've found (using capybara 3.3.0 and chromium driver):

这是我找到的最简洁的方法(使用水豚 3.3.0 和 Chromium 驱动程序):

all('#id-of-select option')[1].select_option

will select the 2nd option. Increment the index as needed.

将选择第二个选项。根据需要增加索引。

回答by Alexandr

In Capybara you can use only findwith xpath

在水豚,你只能使用发现的XPath

find(:xpath, "//*[@id='organizationSelect']/option[2]").click

and method click

和方法点击

回答by David V. Teixeira

It is not a direct answer, but you can (if your server permit):

这不是直接的答案,但您可以(如果您的服务器允许):

1) Create a model for your Organization; extra: It will be easier to populate your HTML.

1) 为您的组织创建模型;额外:填充 HTML 会更容易。

2) Create a factory (FactoryGirl) for your model;

2)为你的模型创建一个工厂(FactoryGirl);

3) Create a list (create_list) with the factory;

3)用工厂创建一个列表(create_list);

4) 'pick' (sample) a Organization from the list with:

4) 从列表中“挑选”(抽样)一个组织,其中:

# Random select
option = Organization.all.sample 

# Select the FIRST(0) by id
option = Organization.all[0] 

# Select the SECOND(1) after some restriction
option = Organization.where(some_attr: some_value)[2]
option = Organization.where("some_attr OP some_value")[2] #OP is "=", "<", ">", so on...