选择:水豚::歧义:歧义匹配,找到2个元素匹配可见选项

时间:2019-01-17 00:23:11

标签: ruby-on-rails selenium-webdriver rspec capybara

我正在尝试从列表中选择一个国家。确实有2个名称相同。

select user_info.company_country, from: 'Company country'

HTML:

<select class="" name="user[company_country]" id="user_company_country">
<option value=""></option>
<option value="United States of America">United States of America</option>
<option value="Afghanistan">Afghanistan</option>
... 200+ countries
<option value="United States of America">United States of America</option>
<option value="Uruguay">Uruguay</option>

错误:

 Capybara::Ambiguous:
   Ambiguous match, found 2 elements matching visible option "United States of America" within #<Capybara::Node::Element tag="select" path="/html/body/div[3]/section/div/div/div/form/div/div[8]/select">

似乎没有选择第一个选项的选项。

https://www.rubydoc.info/github/teamcapybara/capybara/master/Capybara/Node/Actions#select-instance_method

1 个答案:

答案 0 :(得分:4)

与水豚一样,有多种方法可以完成您想要的事情。通过手动找到所需的选项,然后在其上调用select_option,您应该能够做到这一点

find('#user_company_country option[value="United States of America"]', match: :first).select_option

first(:option, 'United States of America').select_option

,或者可能通过传递match: :first选项进行选择(实际上并没有尝试过,但是由于代码在select实际执行的两个发现之间共享,因此在代码中应该可以使用)

select user_info.company_country, from: 'Company country', match: :first