无法找到 Capybara 的复选框

Far*_*ooq 3 checkbox visibility capybara selenium-webdriver

无法找到 Capybara 的复选框。

这是 HTML:

<div class='check-group'>
 <div class='checkbox'>
   <input type="checkbox" name="accepts_fcra" id="accepts_fcra" value="1" />
   <label for="accepts_fcra"><span>Some text <a title="FCRA" target="_blank" href="https://www.google.com/fcra">FCRA</a> some text.</span></label>
 </div>
</div>
Run Code Online (Sandbox Code Playgroud)

现在我想用 ID“accepts_fcra”检查复选框。

我尝试了很多东西,但几乎都返回相同的东西:“无法找到(用于查找复选框的方法)”

一些尝试:

check("#accepts_fcra")
Capybara::ElementNotFound: Unable to find checkbox "#accepts_fcra"

find("#accepts_fcra").set true
Capybara::ElementNotFound: Unable to find css "#accepts_fcra"

within("div.checkbox") do
  find(:xpath, "//input[@id='accepts_fcra']")
end
Capybara::ElementNotFound: Unable to find xpath "//input[@id='accepts_fcra']"
Run Code Online (Sandbox Code Playgroud)

但是checkbox上面的2个类,“.check-group”和“.checkbox”找到了,只是没有找到checkbox。想法?

Far*_*ooq 5

想通了,试过这个:

find("accepts_fcra", :visible => false).click
Run Code Online (Sandbox Code Playgroud)

它起作用了,该元素是“不可见的”,因此只需将其与查找一起传递并单击即可工作。

  • 如果这是为了测试应用程序,则不建议与不可见元素进行交互。此外,并非所有 Capybaras 驱动程序都会单击不可见元素,因此您的测试变得特定于驱动程序,并且将来当您使用的驱动程序切换到不单击不可见元素时可能会中断。相反,您应该执行用户会执行的任何操作,例如单击标签 - `find('label[for="accepts_fcra"]').click` (4认同)