Select 使用 Capybara ElementNotFound 的下拉菜单

Select a Dropdown using Capybara ElementNotFound

我正在尝试 select 来自水豚的下拉菜单中的项目。我使用不同的 CSS 框架做得很好;现在我正在使用 Materialise。

我已经与开发人员谈过,他提到 Materialise 使用两个 select 框,因此可能会让人感到困惑。不确定这是否有帮助,但我想我会提到它。

我正在处理看起来像

的CSS
  <div class="select-wrapper country required">
    <span class="caret">▼</span>    
    <input type="text" class="select-dropdown" readonly="true" data-activates="select-options-0e5c0ffe-1e78-5df0-c08d-7bced194abd1" value="">    

    <ul id="select-options-0e5c0ffe-1e78-5df0-c08d-7bced194abd1" class="dropdown-content select-dropdown" style="width: 435px; position: absolute; top: 0px; left: 0px; opacity: 1; display: none;"><li class="">  <span></span></li>
        <li class=""><span>Afghanistan</span></li>
        <li class=""><span>Åland Islands</span></li>
        <li class=""><span>Albania</span></li>
        <li class=""><span>Algeria</span></li>
        <li class=""><span>United States</span></li></ul>       

    <select class="country required initialized"   name="store[address_attributes][country]" id="store_address_attributes_country"><option value=""></option>
        <option value="AF">Afghanistan</option>
        <option value="AX">Åland Islands</option>
        <option value="AL">Albania</option>
        <option value="DZ">Algeria</option>
        <option value="US">United States</option>

我试过的是

select "United States", :from => 'store_address_attributes_country'

我也试过了

find("store_address_attributes_country").select("United States")

(我也尝试过使用 XPath,select 或名称而不是 ID)

这些给我的错误

 Capybara::ElementNotFound:
   Unable to find select box "store_address_attributes_country`

使用实体化时, 元素和作为下拉列表的

  • 元素。因此,您不能使用 #select。相反,您必须复制用户必须执行的操作,即单击用于触发下拉菜单的输入,然后单击正确的 li。在这种情况下会是

    find("input.select-dropdown").click
    find("li", text: "United States").click
    

    显然,如果页面 select 上有多个 select,则第一个查找必须限定在页面上的某些内容(或增加 selector 的特异性) =]