简体   繁体   English

使用Ruby / Watir选中复选框

[英]Checking a checkbox using Ruby/Watir

I'm trying to automate pulling some data requests from LinkedIn each Friday, but I need to check specific checkboxes to request only relevant data. 我正在尝试每个星期五自动从LinkedIn提取一些数据请求,但是我需要选中特定的复选框以仅请求相关数据。

After going through the documentation and multiple posts I still cannot seem to solve this issue. 在浏览了文档和多篇文章之后,我仍然似乎无法解决此问题。

The specific request page is under LinkedIn's privacy page where you request to download all of your data. 特定的请求页面位于LinkedIn的隐私页面下,您可以在其中请求下载所有数据。

Here's the checkbox line of code: 这是代码的复选框行:

browser.checkbox(id: 'file_group_INVITATIONS').set(true)

I've also tried setting it to false, using the value rather than id as well. 我也尝试过使用值而不是id将其设置为false。

Trying to set the checkbox will give the error: 尝试设置复选框将出现错误:

Selenium::WebDriver::Error::UnknownError : unknown error: Element <input type="checkbox" name="requestedFiles[]" id="file_group_INVITATIONS" value="INVITATIONS"> is not clickable at point (420, 481). Selenium :: WebDriver :: Error :: UnknownError:未知错误:元素<input type =“ checkbox” name =“ requestedFiles []” id =“ file_group_INVITATIONS” value =“ INVITATIONS”>在点(420,481)不可单击。 Other element would receive the click: <label for="file_group_INVITATIONS">...</label> 其他元素将获得点击:<label for =“ file_group_INVITATIONS”> ... </ label>

LinkedIn wanted to style their checkbox, so they actually place a label on top of the actual checkbox element. LinkedIn希望对复选框进行样式设置,因此他们实际上将标签放置在实际复选框元素的顶部。 As a user, when you are clicking the "checkbox", you are actually clicking the label element. 作为用户,当您单击“复选框”时,实际上是在单击标签元素。 This is what the error message is referring to when it talks about another element receiving the click. 这是错误消息在谈到另一个元素获得点击时所指的含义。

You can do the same in Watir. 您可以在Watir中执行相同的操作。 The label is properly associated with the checkbox, which allows you to simply do: 标签与复选框正确关联,您可以简单地执行以下操作:

browser.checkbox(id: 'file_group_INVITATIONS').label.click

Or more directly: 或更直接地:

browser.label(:for => 'file_group_INVITATIONS').click

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

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