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

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


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