Capybara无法选择复选框

发布于 2025-01-30 06:14:04 字数 1001 浏览 1 评论 0原文

我觉得我已经尝试了所有内容,但是我无法在应用程序中选择一个复选框。

我有两个看起来像这样的复选框。一个带有“奶酪”,一个带“泡菜”。它们是由Rails form_with的= 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>

现在要进行我的测试。我尝试了以下内容:

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')

该页面找到复选框。我无法单击标签,因为它是一个链接。当我做save_and_open时,什么都不选择。如何选择复选框?

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

<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.

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. How do I select a checkbox?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂牵梦绕锁你心扉 2025-02-06 06:14:04

您可以使用

查找(“#food_cheese”)。execute_script(“ this.click()”)

page.execute_script(“ document.queryselector)('#food_cheese')。 ”)

You can use

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

or

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文