Capybara的下拉菜单中选择元素

Capybara的下拉菜单中选择元素

本文介绍了从没有ID Capybara的下拉菜单中选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用水豚(红宝石绑定)从没有id或唯一类的下拉列表中选择一个项目,但我似乎无法这样做。

I am trying to use capybara (ruby bindings) to select an item from a dropdown that does not have an id or unique class, but I seem to be unable to do so.

有问题的选择框如下所示:

The select box in question looks like this:

<select data-bind="options: environments, optionsText: 'name', value: selectedEnvironment,   optionsCaption: 'Choose...'" class="form-control">

试着做:

Trying to do a:

select("option",:from "#panel > div.panel-body > form > div:nth-child(1) > select")

不起作用,但我可以使用

does not work, but I am able to find the select field using a

page.find("#panel > div.panel-body > form > div:nth-child(1) > select")

文档说select方法需要选择框的id,name或label,但肯定必须有一种方法从比这更具体的下拉列表中选择一些内容。除了select()之外,还有其他的方法可以使用吗?还是我必须返回使用纯硒?

The documentation says that the select method expects an id, name or label of the select box, but surely there must be a way of selecting something from a dropdown that is more specific than this. Is there another method I can use other than select() or do I have to go back to using pure selenium?

推荐答案

如果您想更多地控制您选择的选项,您需要:

If you want more control over which option you select, you will need to:


  1. 找到选项元素(使用 find all ,等等)

  2. 使用 select_option 方法

  1. Find the option element (using find, all, etc)
  2. Use the select_option method





For example, if you wanted to just select the first option:

option = page.first("#panel > div.panel-body > form > div:nth-child(1) > select option")
option.select_option

或者如果您想选择最后一个:

Or if you wanted to select the last:

options = page.all("#panel > div.panel-body > form > div:nth-child(1) > select option")
options[-1].select_option

这篇关于从没有ID Capybara的下拉菜单中选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:34