select: Capybara::Ambiguous: 歧义匹配,找到 2 个匹配可见选项的元素

select: Capybara::Ambiguous: Ambiguous match, found 2 elements matching visible option

我正在尝试 select 列表中的国家/地区。确实有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

与往常一样,水豚有多种方法可以做你想做的事。你应该能够通过手动找到你想要的选项然后调用 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实际执行)

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