2013-02-21 86 views
0

如何使用ruby和watir单击隐藏的复选框和/或更改隐藏元素的值(从“2”到“1”)?更改隐藏元素的值

的HTML

[div class="spec"] 
    [span class="listheader"]Rechtsgebieden[/span] 
    [div] 
    [span class="legalarea" style="cursor:default" onmouseout="hlt(this,false);" onmouseover="hlt(this,true);" ondblclick="call(this, '.legalarea');" onclick="call(this, '.legalarea');"] 
    [table id="ctl00_cphMC_SS_eJuris_fltrJurLegalArea_cbt" border="0"] [/table] 
    [/div] 
[span class="legalarea-root " style="cursor:default" onmouseout="hlt(this,false);" onmouseover="hlt(this,true);"] 
    [div id="ctl00_cphMC_SS_eJuris_fltrJurLegalArea_qwtA105qwausqwt_pu" class="CheckboxValuePopup" style="display:none;position:absolute;" name="ctl00-cphMC-SS-eJuris-fltrJurLegalArea-qwtA105qwausqwt-pu"] [/div] 
[span class="legalarea-root " style="cursor:default" onmouseout="hlt(this,false);" onmouseover="hlt(this,true);"] 
    [input id="ctl00_cphMC_SS_eJuris_fltrJurLegalArea_qwtA109qwausqwt_cb_cv" type="hidden" value="2" name="ctl00$cphMC$SS$eJuris$fltrJurLegalArea$qwtA109qwausqwt$cb_cv"] 
[img ondblclick="CheckBox(this, '.ctl00-cphMC-SS-eJuris-fltrJurLegalArea-qwtA109qwausqwt-pu', true);" onclick="CheckBox(this, '.ctl00-cphMC-SS-eJuris-fltrJurLegalArea-qwtA109qwausqwt-pu', true);" src="http://portal.rechtsorde.nl/img/2.png"] 
[a class="search-filter-link" onclick="$('.ctl00-cphMC-SS-eJuris-fltrJurLegalArea-qwtA109qwausqwt-pu').dialog('open'); callerID=this;"]Handels- en ondernemingsrecht[/a] 

工程,以访问该元素的代码/查找值:

browser.hidden(:name, /A111/)first.value 
+1

请考虑清理提供的html - (1)html应该使用< and >,而不是[和]; (2)某些元素似乎嵌套不正确或缺少各自的打开/关闭标记,这可能表明html缺失; (3)没有名称为/ A111 /的隐藏元素,它看起来像是动态生成的数字。 – 2013-02-21 17:33:09

+0

会做!谢谢 – Seeb 2013-02-22 11:34:10

回答

3

可以用JS更改值

browser.execute_script('document.ge tElementById(“ctl00_cphMC_SS_eJuris_fltrJurLegalArea_qwtA109qwausqwt_cb_cv”)。value =“1”')

3

的Watir不允许你改变隐藏元素的值。 Watir试图模拟一个实际的用户。由于用户无法更改隐藏元素,Watir也无法更改。相反,您应该尝试与调用该函数的元素交互以更改隐藏的元素。

提供的html似乎有点尴尬(请参阅问题的评论),但我的猜测是,点击看起来像空白复选框字段的img时,隐藏值会发生变化。

你可以尝试一下这是一个同级的隐藏字段的图像:

browser.hidden(:name, /A111/).first.parent.img.click 

鉴于隐藏字段的名称似乎是动态生成的,你可能想尝试以下方法(这我认为,基于不太可能改变的部分):

#Assuming that the link text beside the checkbox is unique 
browser.link(:text => 'Handels- en ondernemingsrecht').parent.img.click 

#Assuming that there is only one hidden element with 'fltrJurLegalArea' in the name: 
browser.hidden(:name, /fltrJurLegalArea/).first.parent.img.click 
+0

第二个人工作。感谢非常清楚,非常有帮助的答案! – Seeb 2013-02-22 11:33:37