简体   繁体   中英

How to verify an attribute is present in an element using Selenium WebDriver?

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.

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).

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

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.

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

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

Look here :

getAttribute(java.lang.String name)

Returns: The attribute's current value or null if the value is not set.

Use whatever test framework you're using to assert that the value is null

Assert.IsNull(getSingleElement(XXX).getAttribute("checked"));

Checkboxes can tricky sometimes, because the attribute checked may 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();

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);
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.

as none of the answers helped me I would like to share the very simple solution I went with in the end:

static ExpectedCondition<Boolean> isAttributePresent(WebElement element, String attrName) {
    JavascriptExecutor executor = (JavascriptExecutor) getDriver();
    String script = "return arguments[0].getAttributeNames();";
    var attributes = executor.executeScript(script, element);
    return input -> ((ArrayList) attributes).contains(attrName);
}

I hope it helps someone. I used it for wait.until(condition) scheme

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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