Java 如何使用 Selenium WebDriver 验证元素中是否存在属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20645013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to verify an attribute is present in an element using Selenium WebDriver?
提问by TestRaptor
I have many radio buttons on my screen. When a radio button is selected, it has an attribute of checked. When the radio button is not selected, the checked attribute is not present. I would like to create a method that would pass if the element is not present.
我的屏幕上有很多单选按钮。当一个单选按钮被选中时,它有一个属性checked。当单选按钮未被选中时,checked 属性不存在。我想创建一个方法,如果元素不存在,该方法将通过。
I am using selenium webdriver and java. I know I can retrieve attributes by using getSingleElement(XXX).getAttribute(XXX)
. I'm just not sure how to verify that an attribute does not exist, and for the test to pass when it doesn't exist (fail if it does exist).
我正在使用 selenium webdriver 和 java。我知道我可以使用getSingleElement(XXX).getAttribute(XXX)
. 我只是不确定如何验证某个属性不存在,以及如何在它不存在时通过测试(如果存在则失败)。
When the radio button is checked
当单选按钮被选中时
<input id="ctl00_cphMainContent_ctl00_iq1_response_0" type="radio" name="ctl00$cphMainContent$ctl00$iq1$response" value="1" checked="checked">
When the radio button is not checked
未选中单选按钮时
<input id="ctl00_cphMainContent_ctl00_iq1_response_0" type="radio" name="ctl00$cphMainContent$ctl00$iq1$response" value="1">
I want the test to pass when the checked attribute is not present
我希望在检查属性不存在时通过测试
采纳答案by Yi Zeng
You can create a method to handle it properly. Note this following is in C#/Java mixed style, you need to tweak a bit to compile.
您可以创建一个方法来正确处理它。注意以下是 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;
}
How to use it:
如何使用它:
WebElement input = driver.findElement(By.cssSelector("input[name*='response']"));
Boolean checked = isAttribtuePresent(input, "checked");
// do your assertion here
回答by Steve Weaver Crawford
Look here:
看这里:
getAttribute(java.lang.String name)
getAttribute(java.lang.String 名称)
Returns:The attribute's current value or null if the value is not set.
返回:属性的当前值,如果未设置该值,则返回null。
Use whatever test framework you're using to assert that the value is null
使用您使用的任何测试框架来断言该值为空
Assert.IsNull(getSingleElement(XXX).getAttribute("checked"));
Assert.IsNull(getSingleElement(XXX).getAttribute("checked"));
回答by pavanraju
For asserting radio button is selected
为了断言单选按钮被选中
Assert.assertTrue(element.isSelected());
For asserting radio button is not selected
未选择断言单选按钮
Assert.assertFalse(element.isSelected());
For asserting an attribute is present in element
用于断言元素中存在属性
Assert.assertEquals(element.getAttribute(attributeName), expectedAttributeValue);
回答by PaoloV
Checkboxes can tricky sometimes, because the attribute checkedmay not be followed by an attribute value.
复选框有时会很棘手,因为检查的属性后面可能没有属性值。
If you're only concerned with the presence of the attribute, a simple check would look like this:
如果您只关心属性的存在,一个简单的检查看起来像这样:
boolean hasAttr_css = driver.findElementsByCssSelector("#input_id[checked]").isEmpty();
回答by PaoloV
try {
if (webdriver.findElement(By.identificationMethod(value)).getAttribute(value) != null) {
passed = false;
}
} catch (Exception e) {
passed = true;
}
Was the sort of solution I used when I needed to check for element presence, however it must be noted that NullPointer errors were not caught by the NoSuchElement exception.
这是我在需要检查元素是否存在时使用的那种解决方案,但是必须注意,NoSuchElement 异常未捕获 NullPointer 错误。
回答by mehmetg
Unfortunately the accepted answer is not valid in the case reported. For some reason for Cr and FF non-existing attributes return empty string rather than null. Issue linked: https://github.com/SeleniumHQ/selenium/issues/2525
不幸的是,接受的答案在报告的案例中无效。由于某些原因,Cr 和 FF 不存在的属性返回空字符串而不是 null。问题链接:https: //github.com/SeleniumHQ/selenium/issues/2525