选择不可见的单选按钮

时间:2018-08-09 08:28:41

标签: ruby web-scraping watir

我正在使用ruby 2.5.1,watir 6.11,Chrome 68.0,chrome webdriver 2.40进行网页抓取,我在选择单选按钮时遇到了问题。我页面的HTML代码如下:

<tr> <td class="filtr-info">&nbsp;</td>
<td class="filtr-content-radio">
<div class="iradio_square-grey checked" style="position: relative;">
<input id="id1" name="name" type="radio" value="0" checked="checked" style="position: absolute; opacity: 0;">  
<ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0; cursor: pointer;">
</ins></div>
<span class="filter-radio-name">KRS</span>
<div class="iradio_square-grey" style="position: relative;">
<input id="id2" name="name" type="radio" value="1" style="position: absolute; opacity: 0;">
<ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0; cursor: pointer;">
</ins></div>
<span class="filter-radio-name">REGON</span>
<div class="iradio_square-grey" style="position: relative;">
<input id="id3" name="name" type="radio" value="2" style="position: absolute; opacity: 0;">
<ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0; cursor: pointer;">
</ins></div>
<span class="filter-radio-name">NIP</span>
</td>
<td class="filtr-help">&nbsp;</td>
<td>&nbsp;</td>
</tr>

因此,我有3个单选按钮,并希望选择其中一个。当我尝试对其中的方法使用 set 时,会出现以下错误:

unknown error: Element <input id="id1" name="name" type="radio" value="1" style="position: absolute; opacity: 0;"> is not clickable at point (623, 245).

我还尝试了一些方法来检查watir如何看待这些按钮:

$b.radio(:id,'id1').exists? => true
$b.radio(:id,'id1').present? => false
$b.radio(:id,'id1').visible? => false
$b.radio(:id,'id1').hidden? => false

如果按钮仅存在但不可见或不存在,有什么方法可以选择其中之一?

2 个答案:

答案 0 :(得分:1)

由于ins元素覆盖了您的元素,并且我认为它在html中是永久性的,因此您最好执行此点击,希望它能起作用。

b.radio(id: 'id2').following_sibling.click

答案 1 :(得分:0)

您可以遍历单选按钮,检查它是present?,然后是click还是set

['id1', 'id2', 'id3'].each do |el_id|
   el = $b.radio(id: el_id)
   el.click if el.present?
end