[英]htmlunit : checkbox not getting checked
I am using HtmlUnit for setting a checkbox in a page.我正在使用 HtmlUnit 在页面中设置复选框。 Html code for checkbox is :复选框的 HTML 代码是:
<input id="Checkbox" type="checkbox" name="Checkbox" style="color:Black;"/>
My java code to set checkbox is :我设置复选框的java代码是:
HtmlCheckBoxInput checkBox = page.getHtmlElementById("Checkbox");
checkBox.setChecked(true);
FileUtils.writeStringToFile(new File("src/test/page-dumps/page-3.html"),page.asXml(),"UTF-8");
When I open page-3.html
in my browse, ckeckbox is unchecked.当我在浏览器中打开page-3.html
时,未选中 ckeckbox。 Why is it not checked?为什么不检查?
Well, it is not checked because real browsers don't add an attribute.好吧,它没有被检查,因为真正的浏览器不会添加属性。
Test the below with real browser:使用真实浏览器测试以下内容:
<html><head>
<script>
function test() {
var e = document.getElementById('myid');
e.checked = true;
alert(e.outerHTML);
}
</script>
</head><body onload="test()">
<input type=checkbox id=myid>
</body></html>
There is an alert of有一个警报
<input type="checkbox" id="myid">
without any checked
attribute.没有任何checked
属性。
You should instead use:你应该使用:
checkBox.setAttribute("checked", "checked");
您可以尝试将checked
属性添加到<input>
元素吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.