如何使用水豚下拉菜单中选择选项 [英] How to select option in drop down using Capybara

查看:93
本文介绍了如何使用水豚下拉菜单中选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to select an item from a drop down menu using 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

但是我不想使用b/c方法,该值会改变并且会使我的测试变脆.

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.

下拉菜单的HTML是:

The HTML for the drop down is:

<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>

我也尝试过:

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

但这会导致此错误:

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

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

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

推荐答案

如果您查看

If you take a look at the source of the select method, you can see that what it does when you pass a from key is essentially:

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

换句话说,它将找到您感兴趣的<select>,然后在其中找到<option>,然后在<option>节点上调用select_option.

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

您已经完成了前两件事,我只是重新排列了一下.然后,您可以在最后添加select_option方法:

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

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

这篇关于如何使用水豚下拉菜单中选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆