TestRaptor
TestRaptor

Reputation: 1315

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

Upvotes: 25

Views: 92493

Answers (7)

Krzysztof Nowicki
Krzysztof Nowicki

Reputation: 100

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

Upvotes: 0

Yi Zeng
Yi Zeng

Reputation: 32855

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

Upvotes: 36

mehmetg
mehmetg

Reputation: 307

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

Upvotes: 5

user3864935
user3864935

Reputation:

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.

Upvotes: 1

PaoloV
PaoloV

Reputation: 126

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

Upvotes: 3

pavanraju
pavanraju

Reputation: 673

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

Upvotes: 2

Steve Weaver Crawford
Steve Weaver Crawford

Reputation: 1059

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

Upvotes: 4

Related Questions