简体   繁体   English

水豚不能选中复选框

[英]Capybara cannot select checkbox

I feel like I have tried everything, but I cannot select a checkbox in my application.我觉得我已经尝试了一切,但我无法在我的应用程序中选择一个复选框。

I have two checkboxes that look like this.我有两个看起来像这样的复选框。 One with 'cheese' and one with 'pickles'.一种是“奶酪”,一种是“泡菜”。 they're auto generated with Rails form_with's = form.check_box它们是使用 Rails form_with's = form.check_box自动生成的

<input name="foods[]" type="hidden" value="0" autocomplete="off">
<input class="foodCheckbox" type="checkbox" value="cheese" name="food[]" id="food_cheese">
<a target="_blank" href="MyString">cheese</a>

Now to my testing.现在到我的测试。 I have tried the following:我尝试了以下方法:

1. 1.

check('food_cheese')
check('#food_cheese')
find("food_cheese").click
find("#food_cheese").click
find("food_cheese", visible: false).click
find("#food_cheese", visible: false).click
find(:css, "#food_cheese").set(true)
find(:css, "#food_cheese", visible: false).execute_script('this.checked = true')

The page finds the checkboxes.该页面找到复选框。 I cannot click the label because it is a link.我无法单击标签,因为它是一个链接。 When I do save_and_open nothing is selected.当我执行save_and_open时,没有选择任何内容。 How do I select a checkbox?如何选择复选框?

You can use您可以使用

find("#food_cheese").execute_script("this.click()")

or或者

page.execute_script("document.querySelector('#food_cheese').click()")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM