简体   繁体   English

如何使用红宝石水豚/硒访问复选框

[英]How do I access a checkbox with ruby capybara/selenium

My usual method of obtaining a checkbox doesn't appear to be working here... Basically I have a table with some data in it but the first td is a checkbox. 我通常使用的获取复选框的方法在这里似乎不起作用...基本上我有一个包含一些数据的表,但第一个td是一个复选框。 What I am trying to do is click on that checkbox, yet no mater what I do, it seems to not be present on the screen or at least it is not clickable. 我想做的是单击该复选框,但是我没有做任何事情,它似乎没有出现在屏幕上,或者至少它不可单击。 I've tried using xpath with td[1] , td[1]/input , td[1]/input[@data-role="checkbox"] and a number of other permutations but no luck... I am using SitePrism and within the row already so I should be able to get right to these elements. 我尝试将xpath与td[1]td[1]/inputtd[1]/input[@data-role="checkbox"]和许多其他排列方式结合使用,但是没有运气...我正在使用SitePrism并在行内,因此我应该能够正确使用这些元素。 For example using td[2] with .text gives me Foo no problem... But it is almost as if the checkbox is not there. 例如,将td[2].text ,就不会给我Foo任何麻烦……但是,几乎就像复选框不存在一样。

Additionally if I try doing all('input').size or all('label').size I am getting 0, which indicates to me that those aren't rendering despite clearly being displayed on the page and inspected via chrome dev tools. 另外,如果我尝试执行all('input').sizeall('label').size我将得到0,这表示尽管显示在页面上并已通过chrome开发工具进行了检查,但它们仍未呈现。

<tr data-uid="123" role="row" class="state-selected" tabindex="1008">
    <td aria-describedby="75e50a2e-9354-43aa-90af-231170f41b4f" role="gridcell">
        <input class="k-checkbox" data-role="checkbox" id="3a3d214a-9e48-4c3e-869d-9821b42ded19" aria-label="Select row" aria-checked="false" type="checkbox" tabindex="-1">
        <label for="3a3d214a-9e48-4c3e-869d-9821b42ded19" class="k-checkbox-label k-no-text">&#8203;</label>
    </td>
    <td class="gridDataString" tabindex="-1" aria-describedby="6e767f1c-78a3-4988-9633-17bcab112766" role="gridcell">
        <span>Foo</span>
    </td>
    <td class="gridDataString" tabindex="-1" aria-describedby="2f654308-1ddb-4f54-a3bf-2d6c36980378" role="gridcell">
        <span>Bar</span>
    </td>
    <td class="gridDataString" tabindex="-1" aria-describedby="3ae4ac0a-e16d-4892-b8ad-a821adeb55ee" role="gridcell">
        <span></span>
    </td>
    <td class="gridDataString" tabindex="-1" aria-describedby="f443994b-3e0f-4e8e-a658-f5efe839bb5e" role="gridcell">
        <span>Foo Barrr</span></td>
    <td class="gridDataString" tabindex="-1" style="display:none" aria-describedby="0f49ae1e-765e-4f99-83b1-46aa3067ac5f" role="gridcell">
        <span ng-bind="dataItem.modelId">0x0000000000004701,0x00000000000811f9,</span>
    </td>
</tr>

I'm guessing the actual checkbox element isn't visible on the page and has been hidden via CSS to replace with an image for styling reasons. 我猜测实际的复选框元素在页面上不可见,并且出于样式原因已通过CSS隐藏以替换为图像。 Check the styles applied to the actual checkbox element for display: none , opacity: 0 , etc. 检查应用于实际复选框元素的样式以进行display: noneopacity: 0等。

To work with this you can either click the label element associated with the checkbox or pass the allow_label_click element to check to have it click the associated label when the actual checkbox is unclickable. 要使用此功能,您可以单击与复选框关联的标签元素,或者通过allow_label_click元素进行check以确保在无法单击实际复选框时单击关联的标签。 Another potential option if there isn't a visible associated label element is to just click the td element - which will trigger a click in the middle of the td which should actually hit the checkbox replacement. 如果没有可见的关联标签元素,则另一个可能的选择是单击td元素-这将触发td中间的单击,这实际上应该是复选框替换。 Assuming you have the row element already ( tr ) one of the following should work 假设您已经具有row元素( tr ),则应执行以下操作之一

tr.check(class: 'k-checkbox', allow_label_click: true)
tr.find('label.k-checkbox-label').click

Not as understandable but may work/be necessary in some cases 难以理解,但在某些情况下可能有用/必要

tr.find('td:first-child').click

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

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