水豚,从下拉菜单中选择第一个选项? [英] Capybara, selecting 1st option from dropdown?

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

问题描述

我已经进行了搜索,并且大多数相关的google搜索结果都已返回,通常只是从下拉菜单中选择一个元素。但是不幸的是,在这种情况下,下拉列表中的元素的ID是动态生成的。

I've done a search and most of the related google results have returned just in general selecting an element from a dropdown. However the ID's in this case for the elements in the dropdown are dynamically generated unfortunately.

这是针对基本测试用例的,因此我基本上只需要选择例如第一。下拉菜单中的元素文本也相同(不确定是否有帮助)。

This is for a base test case, so I basically just need to select for example the first one. The text is also the same for the elements in the dropdown (not sure if that helps).

有这样的例子吗?

我将黄瓜与大羚羊结合使用(使用硒驱动器)

Im using cucumber with caybara(using selenium driver) integration

推荐答案

您可以找到第一个选项元素,然后使用 select_option 方法将其选中。

You can find the first option element and then use the select_option method to select it.

例如,如果选择列表的ID为 select_id,您可以执行以下操作:

For example, if the select list has an id "select_id", you can do:

first('#select_id option').select_option

正如@TomWalpole所提到的,这不会等待元素出现。进行以下操作之一会更安全:

As @TomWalpole mentions, this will not wait for the element to appear. It would be safer to do one of the following:

first('#select_id option', minimum: 1).select_option

find('#select_id option:first-of-type').select_option

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

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