铁路Capybara点击带有链接的标签

发布于 2025-02-06 14:44:05 字数 1268 浏览 2 评论 0原文

在我的RSPEC功能中,我尝试使用相关标签检查一个复选框,因为输入是隐藏的。我的标签包含链接(< a> tag),因此每次我使用check> check> check('user [clase_terms]',lashe_label_click:true)>而是打开标签中包含的链接。

这是我从erb生成的我的视图,

    <p class="col boolean required user_agree_terms">
      <input name="user[agree_terms]" type="hidden" value="0">
      <input class="boolean required" required="required" aria-required="true" type="checkbox" value="1" name="user[agree_terms]" id="user_agree_terms">
      <label class="boolean required" for="user_agree_terms">
        <abbr title="required">*</abbr>
        J'ai accepté les <a target="_blank" href="https://somelink">Conditions Générales d'Utilisation</a> et la <a target="_blank" href="https://somelink">Politique de Confidentialité</a>
      </label>
    </p>

这是我的规格,

  # I tried both but its behave the same way : open the <a> tag
  find('label[for="influencer_agree_terms"]').click
  check('influencer[agree_terms]', allow_label_click: true)

您知道另一种选中该框的方式吗?

顺便说一下,当我单击标签时,浏览器上的UI正常工作。

in my rspec feature I try to check a checkbox with it related label because the input is hidden. My label contains link (<a> tag), so every time I use check('user[agree_terms]', allow_label_click: true) it never checks the box but instead open the link contains in the label.

Here's my view generated from ERB

    <p class="col boolean required user_agree_terms">
      <input name="user[agree_terms]" type="hidden" value="0">
      <input class="boolean required" required="required" aria-required="true" type="checkbox" value="1" name="user[agree_terms]" id="user_agree_terms">
      <label class="boolean required" for="user_agree_terms">
        <abbr title="required">*</abbr>
        J'ai accepté les <a target="_blank" href="https://somelink">Conditions Générales d'Utilisation</a> et la <a target="_blank" href="https://somelink">Politique de Confidentialité</a>
      </label>
    </p>

Here's my spec

  # I tried both but its behave the same way : open the <a> tag
  find('label[for="influencer_agree_terms"]').click
  check('influencer[agree_terms]', allow_label_click: true)

Do you know another way to check that box ?

By the way the UI on browser work correctly when I click the label.

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

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

发布评论

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

评论(2

咽泪装欢 2025-02-13 14:44:06

当您告诉Capybara单击一个元素时,单击该元素的中间,在这种情况下,该元素具有条件链接,因此请在标签元素之前单击。一种选项是专门单击标签中的ABBR元素,但远离链接的另一个选项是,

find('label[for="influencer_agree_terms"] abbr').click

另一个选项是利用允许_label_click支持为单击位置指定偏移的事实,

check('influencer[agree_terms]', allow_label_click: { x: 10, y: 10 })

具体取决于capybara的设置.w3c_click_offset该偏移量可以来自中心或标签元素的左上角,因此请根据需要调整值。 执行相同的操作

find('label[for="influencer_agree_terms"]').click(x: 10, y: 10)

从技术上讲,您可以在调用单击时可以

When you tell Capybara to click on an element it clicks in the middle of that element which in this case is where you have the conditions link, so that takes the click before the label element. One option would be to specifically click on the abbr element, which is in the label but away from the link

find('label[for="influencer_agree_terms"] abbr').click

Another option would be to take advantage of the fact that allow_label_click supports specifying offsets for the click location

check('influencer[agree_terms]', allow_label_click: { x: 10, y: 10 })

Depending on the setting of Capybara.w3c_click_offset that offset could be from the center or from the top left corner of the label element so adjust the values as necessary. Technically you can do the same thing when calling click

find('label[for="influencer_agree_terms"]').click(x: 10, y: 10)

but it's generally better to use check when dealing with a checkbox

﹉夏雨初晴づ 2025-02-13 14:44:06

默认情况下,Capybara #click方法将在我的链接所在的位置单击元素的中间。
幸运的是#click具有偏移选项,如下#Click(x:0,y:0)

因此,在包含链接的标签的帮助下检查一个框:
查找('label [for =“ fallencer_agree_terms”]')。单击(x:0,y:0)

By default capybara #click method will click in the middle of the element, right where my link is located.
Fortunately #click has offset option as follow #click(x: 0, y: 0).

So to check a box with the help of its label containing link :
find('label[for="influencer_agree_terms"]').click(x: 0, y: 0)

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