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

Chl*_*loe 2 rspec ruby-on-rails capybara selenium-webdriver

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

select user_info.company_country, from: 'Company country'
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

错误:

 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">
Run Code Online (Sandbox Code Playgroud)

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

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

Tho*_*ole 5

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

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

要么

first(:option, 'United States of America').select_option
Run Code Online (Sandbox Code Playgroud)

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

select user_info.company_country, from: 'Company country', match: :first
Run Code Online (Sandbox Code Playgroud)