Tes*_*tor 17 java selenium webdriver selenium-webdriver
我的屏幕上有很多单选按钮.选择单选按钮时,它具有已检查的属性.未选择单选按钮时,不存在checked属性.我想创建一个方法,如果元素不存在将通过.
我正在使用selenium webdriver和java.我知道我可以通过使用来检索属性getSingleElement(XXX).getAttribute(XXX).我只是不确定如何验证属性不存在,以及测试在不存在时传递(如果确实存在则失败).
选中单选按钮时
<input id="ctl00_cphMainContent_ctl00_iq1_response_0" type="radio" name="ctl00$cphMainContent$ctl00$iq1$response" value="1" checked="checked">
Run Code Online (Sandbox Code Playgroud)
未选中单选按钮时
<input id="ctl00_cphMainContent_ctl00_iq1_response_0" type="radio" name="ctl00$cphMainContent$ctl00$iq1$response" value="1">
Run Code Online (Sandbox Code Playgroud)
我希望在checked属性不存在时传递测试
Yi *_*eng 23
您可以创建一个方法来正确处理它.注意以下是C#/ Java混合风格,需要调整一下才能编译.
private boolean isAttribtuePresent(WebElement element, String attribute) {
Boolean result = false;
try {
String value = element.getAttribute(attribute);
if (value != null){
result = true;
}
} catch (Exception e) {}
return result;
}
Run Code Online (Sandbox Code Playgroud)
如何使用它:
WebElement input = driver.findElement(By.cssSelector("input[name*='response']"));
Boolean checked = isAttribtuePresent(input, "checked");
// do your assertion here
Run Code Online (Sandbox Code Playgroud)
不幸的是,在报告的情况下,接受的答案无效。由于某些原因,Cr和FF不存在的属性返回空字符串而不是null。链接的问题:https : //github.com/SeleniumHQ/selenium/issues/2525
| 归档时间: |
|
| 查看次数: |
49879 次 |
| 最近记录: |