在 Capybara 中单击带有滚动条的下拉菜单

Clicking in a dropdown with scroll in Capybara

I'm having a problem with my dropdown, It uses selectize to make the dropdown, but when the number of options is too big it adds a scroll to the dropdown and when I try to click in some option that is没有看到(你需要滚动它才能看到)水豚认为这个选项在那里,然后在没有滚动的情况下点击选项所在的输入。可见性没有任何变化(命令它搜索不可见元素也不起作用)

您可以单击 drop-down 中的可见元素,然后发送 :arrow_down 本机键来模拟下键操作。您应该这样做直到元素可见,然后单击 active 选项。

检查 selectize.js 主页:

find("#select-country-selectized").click()
while(true)
  break if find(".option.active").text == "Benin"
  find("#select-country-selectized").native.send_keys(:arrow_down)
end

find(".option.active").click

gunesmes 的例子非常有帮助,我做了一些更改,比如将 break 排成一行,否则它总是会跳到第一个选项并更改 has_css 的查找?因为发现 returns 一个错误和 has_css? returns 对或错。我还更改了第一个 find,因为我使用 cocoon 并且我总是需要填充最后生成的输入。最后的结果是这样的:

    def scroll_dropdown(user)
     all('input[id$="_user_id-selectized"]').last.click
     while(true)
      break if page.has_css?(".option.active", text: user, match: :prefer_exact, wait: false)
      all('input[id$="_user_id-selectized"]').last.native.send_keys(:arrow_down)
     end

     find(".option.active").click
    end