ruby Capybara:按值而不是文本选择一个选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12265174/
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
Capybara: Select an option by value not text
提问by Paul
For the HTML
对于 HTML
<select id="date">
<option value="20120904">Tue 4 Sep 2012</option>
<option value="20120905">Wed 5 Sep 2012</option>
<option value="20120906">Thu 6 Sep 2012</option>
</select>
I have the following Capybara Ruby code:
我有以下 Capybara Ruby 代码:
select "20120905", :from => "date"
But this errors with:
但是这个错误:
cannot select option, no option with text '20120905' in select box 'date' (Capybara::ElementNotFound)
However, if I do
但是,如果我这样做
select "Wed 5 Sep 2012", :from => "date"
It's ok.
没关系。
Is it possible to select an option in Capybara by Valuenot Text?
是否可以通过Value而不是Text在 Capybara 中选择一个选项?
Thanks
谢谢
回答by robynhenderson
This will work to select an option by value:
这将用于按值选择一个选项:
find("option[value='20120905']").click
To maintain the scope of the selector you could wrap it in a within block as such:
为了保持选择器的范围,您可以将它包装在一个内块中,如下所示:
within '#date' do
find("option[value='20120905']").click
end
回答by Mark
With Poltergeist as driver I can't click on an option like suggested in some of the other options above, instead you can do the following:
使用 Poltergeist 作为驱动程序,我无法单击上面其他一些选项中建议的选项,而是可以执行以下操作:
page.find_by_id('date').find("option[value='20120905']").select_option
page.find_by_id('date').find("option[value='20120905']").select_option
回答by d_rail
I wrote a helper method:
我写了一个辅助方法:
def select_by_value(id, value)
option_xpath = "//*[@id='#{id}']/option[@value='#{value}']"
option = find(:xpath, option_xpath).text
select(option, :from => id)
end
Save in a .rb file in spec/support/
保存在spec/support/中的 .rb 文件中
Example use:
使用示例:
before do
select_by_value 'some_field_id', 'value'
click_button 'Submit'
end
回答by TrashyMcTrash
You can also achieve it by doing the following:
您还可以通过执行以下操作来实现它:
find_by_id('date').find("option[value='20120905']").click
回答by GeneK
That helper method is pretty clever. I would change it just a little bit:
这个辅助方法非常聪明。我会稍微改变一下:
def select_by_value(id, value)
option_xpath = "//*[@id='#{id}']/option[@value='#{value}']"
find(:xpath, option_xpath).click
end
or just:
要不就:
find(:xpath, "//select[@id='date']/option[@value='20120904']").click
回答by iRet
In my case I have a few options with same text, that's the reason why I need select by value. Combining a few answers together I've found the best solution for me:
在我的情况下,我有几个具有相同文本的选项,这就是我需要按值选择的原因。结合几个答案,我找到了最适合我的解决方案:
def select_by_value(id, value)
option_xpath = "//*[@id='#{id}']/option[@value='#{value}']"
find(:xpath, option_xpath).select_option
end
回答by steel
You could also use capybara-uiwhich will look first to match the text, then to match the value.
您还可以使用capybara-ui,它首先查找匹配文本,然后匹配值。
# define your form widget, in this case in a role
class UserRole < Capybara::UI::Role
form :my_form do
select :my_select, 'my_select'
end
end
# then just submit your form params via #submit
role = UserRole.new
role.submit :my_form, my_select: '20120905'
See more about capybara-ui forms here.
在此处查看有关水豚-ui 表单的更多信息。
回答by cage
Click using find_field works fine:
单击使用 find_field 工作正常:
find_field("date").find("option[value='20120905']").click

