黄瓜:下拉菜单应该选择项目吗? xpath 怎么样?或者也许是别的什么?

发布于 2024-12-06 04:06:41 字数 399 浏览 0 评论 0原文

<select id="search_user_id_equals_any" name="search[user_id_equals_any]">
    <option value="2">My Stuff</option>
    <option value="-1,1,2,3,4,5">All Users</option>
    <option value="3">The Cat</option>
</select>

所以,上面是我想要验证的下拉菜单。

现在,当您单击某些内容时,它不会将 selected="selected" 添加到您单击的任何选项中,所以我不知道如何计算出选择。

想法?

<select id="search_user_id_equals_any" name="search[user_id_equals_any]">
    <option value="2">My Stuff</option>
    <option value="-1,1,2,3,4,5">All Users</option>
    <option value="3">The Cat</option>
</select>

So, above is the drop down I want to verify something is selected in.

Now, when you click on something, it doesn't add the selected="selected" to whatever option you click, so I don't know how to figure out the selection.

ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿挽旧人 2024-12-13 04:06:41

怎么样:

   find_field(search_user_id_equals_any).value.should =~ /#{your_expected_value}/

How about:

   find_field(search_user_id_equals_any).value.should =~ /#{your_expected_value}/

?

只为守护你 2024-12-13 04:06:41

您可以从 Capybara 源码 中看到value 方法的工作原理:

option = native.xpath(".//option[@selected='selected']").first || native.xpath(".//option").first
option[:value] || option.content if option

所以看起来按照设计,它将返回选项的值(如果存在),否则它将返回文本内容。请注意,如果未选择任何选项,它将默认为第一个,就像真正的浏览器一样。

为了获得你想要的行为,你可以这样做:

node = find_field('search_user_id_equals_any')
option = node.xpath(".//option[@selected='selected']").first || node.xpath(".//option").first
option_text = option.content

You can see from the Capybara source how the value method works:

option = native.xpath(".//option[@selected='selected']").first || native.xpath(".//option").first
option[:value] || option.content if option

So it looks like by design it will return the option's value, if present, and otherwise it will return the text content. And note how if no option is selected, it will default to the first, as a real browser would.

To get the behaviour you want, you could do something like this:

node = find_field('search_user_id_equals_any')
option = node.xpath(".//option[@selected='selected']").first || node.xpath(".//option").first
option_text = option.content
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文