简体   繁体   English

Watir:在复选框中选择一个项目

[英]Watir: Select an item in checkbox

I am using watir-webdriver, I am trying to select one box from the HTML code below, for example "LC" for the input but I am having some troble to get it work. 我正在使用watir-webdriver,我试图从下面的HTML代码中选择一个框,例如输入“ LC”,但是我遇到了一些麻烦使其工作。 Anyone have any idea please. 任何人都有任何想法。 thanks 谢谢

browser.checkbox(:value => 'LC').set

error message C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-2.0.4/lib/watir/element.rb:78:in `asse rt_exists': Unable to locate element, using {:value=>"LC"} (Watir::Exception::Un knownObjectException) 错误消息C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-2.0.4/lib/watir/element.rb:78:在'asse rt_exists'中:无法使用{:value来定位元素=>“ LC”}(Watir :: Exception :: UnknownObjectException)

code

<TD align="right">Reason:&nbsp;&nbsp;</TD>
                    <TD><style type="text/css"> 
select, ul { height: 40px; overflow: auto; width: 205px; border: 1px solid #000; }
ul { list-style: none; margin: 0; padding: 0; overflow-x: hidden;}
li { margin: 0; padding: 0; }
ul li {
position: relative;
}
li ul {
position: absolute;
left: 149px;
top: 0;
display: none;
}
label { display: block; color: WindowText; background-color: Window; margin:0; 
padding:0; width: 100%; }
label:hover { background-color: Highlight; color: HighlightText; display: block; }
</style>

<script type="text/javascript"> 
function appendValue(checkedValue) {
findFormElement("reason").value = ""
if(document.getElementById("CS").checked){
findFormElement("reason").value= findFormElemen"reason").value+document.getElementById   ("CS").id;
}
if(document.getElementById("LC").checked){
findFormElement("reason").value = findFormElement("reason").value + document.getElementById("LC").id;
}
if(document.getElementById("ND").checked){
    findFormElement("reason").value = findFormElement("reason").value +  document.getElementById("ND").id;
}
if(document.getElementById("NG").checked){
    findFormElement("reason").value = findFormElement("reason").value + document.getElementById("NG").id;
}
if(document.getElementById("NC").checked){
    findFormElement("reason").value = findFormElement("reason").value + document.getElementById("NC").id;
}
if(document.getElementById("WD").checked){
findFormElement("reason").value = findFormElement("reason").value + document.getElementById("WD").id;
}
};
</script>
<input name="reason" type="hidden" value></select><ul id="multisel">
<li><label for="CS"><input type="checkbox" name="CS" id="CS" onClick="appendValue   (&quot;CS&quot;)"/>Cannot Supply Within 2 Hours</label></li>
<li><label for="LC"><input type="checkbox" name="LC" id="LC" onClick="appendValue(&quot;LC&quot;)"/>Location Closed</label></li>
<li><label for="ND"><input type="checkbox" name="ND" id="ND" onClick="appendValue(&quot;ND&quot;)"/>No Drivers Available</label></li>
<li><label for="NG"><input type="checkbox" name="NG" id="NG" onClick="appendValue(&quot;NG&quot;)"/>No Vehicle Group Available</label></li>
<li><label for="NC"><input type="checkbox" name="NC" id="NC" onClick="appendValue(&quot;NC&quot;)"/>No Vehicles Available</label></li>
<li><label for="WD"><input type="checkbox" name="WD" id="WD" onClick="appendValue(&quot;WD&quot;)"/>Weekend Delivery</label></li>
</ul></TD>

select, ul { height: 40px; overflow: auto; width: 205px; border: 1px solid #000; }
ul { list-style: none; margin: 0; padding: 0; overflow-x: hidden;}
li { margin: 0; padding: 0; }
ul li {
position: relative;
}
li ul {
position: absolute;
left: 149px;
top: 0;
display: none;
}
label { display: block; color: WindowText; background-color: Window; margin: 0;   padding: 0; width: 100%; }
label:hover { background-color: Highlight; color: HighlightText; display: block; }
</style>

<script type="text/javascript"> 
function appendValue(checkedValue) {
findFormElement("reason").value = ""
if(document.getElementById("CS").checked){
findFormElement("reason").value = findFormElement("reason").value +  document.getElementById("CS").id;
}
if(document.getElementById("LC").checked){
    findFormElement("reason").value = findFormElement("reason").value + document.getElementById("LC").id;
}
if(document.getElementById("ND").checked){
    findFormElement("reason").value = findFormElement("reason").value + document.getElementById("ND").id;
}
if(document.getElementById("NG").checked){
    findFormElement("reason").value = findFormElement("reason").value +  document.getElementById("NG").id;
}
if(document.getElementById("NC").checked){
    findFormElement("reason").value = findFormElement("reason").value + document.getElementById("NC").id;
}
if(document.getElementById("WD").checked){
    findFormElement("reason").value = findFormElement("reason").value + document.getElementById("WD").id;
}
};
</script>
<input name="reason" type="hidden" value></select><ul id="multisel">
<li><label for="CS"><input type="checkbox" name="CS" id="CS" onClick="appendValue   (&quot;CS&quot;)"/>Cannot Supply Within 2 Hours</label></li>
<li><label for="LC"><input type="checkbox" name="LC" id="LC" onClick="appendValue(&quot;LC&quot;)"/>Location Closed</label></li>
<li><label for="ND"><input type="checkbox" name="ND" id="ND" onClick="appendValue(&quot;ND&quot;)"/>No Drivers Available</label></li>
<li><label for="NG"><input type="checkbox" name="NG" id="NG" onClick="appendValue(&quot;NG&quot;)"/>No Vehicle Group Available</label></li>
<li><label for="NC"><input type="checkbox" name="NC" id="NC" onClick="appendValue(&quot;NC&quot;)"/>No Vehicles Available</label></li>
<li><label for="WD"><input type="checkbox" name="WD" id="WD" onClick="appendValue(&quot;WD&quot;)"/>Weekend Delivery</label></li>
</ul></TD>

The checkboxes don't have a value; 复选框没有值。 however they each have an id . 但是它们每个都有一个id Try 尝试

browser.checkbox(:id => 'LC').set 

如果您最终想要取消选择复选框,则该方法很明确

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

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