铁路Capybara点击带有链接的标签
在我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您告诉Capybara单击一个元素时,单击该元素的中间,在这种情况下,该元素具有
条件
链接,因此请在标签元素之前单击。一种选项是专门单击标签中的ABBR元素,但远离链接的另一个选项是,另一个选项是利用允许_label_click支持为单击位置指定偏移的事实,
具体取决于
capybara的设置.w3c_click_offset
该偏移量可以来自中心或标签元素的左上角,因此请根据需要调整值。 执行相同的操作从技术上讲,您可以在调用单击时可以
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 linkAnother option would be to take advantage of the fact that allow_label_click supports specifying offsets for the click location
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 clickbut it's generally better to use
check
when dealing with a checkbox默认情况下,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)