Den*_*yba 4 javascript checkbox jquery selenium selenium-webdriver
我正在尝试使用 Selenium Webdriver 或 Javascript 检查复选框的状态,但经过大量研究后我仍然无法做到。
我的问题是:复选框没有“checked”属性:
<input type="checkbox" name="site[new_sign_up]" id="site_new_sign_up" value="1">
Run Code Online (Sandbox Code Playgroud)
对于常规的普通复选框,我使用下一个字符串来检测复选框是否被选中:
if (checkbox.GetAttribute("checked") != null && checkbox.GetAttribute("checked").Equals("true"))
Run Code Online (Sandbox Code Playgroud)
我知道可以用JS来完成:
$get("isAgeSelected").checked == true
Run Code Online (Sandbox Code Playgroud)
但我仍然无法做到这一点,因为我的复选框没有“选中”属性。
如果使用硒,我检查元素的“选定”属性,它也不会告诉我复选框状态的真相。
关于如何做有什么建议吗?提前致谢。
DOM API 为所有输入类型提供了选中属性,无论它们是否具有选中属性(或者即使它们不可选中,即文本元素)
您不应该依赖存在的选中属性来确定复选框是否被选中。
var x = document.createElement('input');
console.log(x.checked); //false
x.type = 'checkbox';
console.log(x.checked); //false
x.checked = true;
console.log(x.checked); //true
console.log(x); //<input type="checkbox"> - see? no checked attribute, yet it is still checked
Run Code Online (Sandbox Code Playgroud)