Search code examples
javaseleniumselenium-webdriverwebdrivergetattribute

How to verify if WebElement is selected or not


I want to verify if WebElement is selected or not. This element exist (when is selected) in HTML as:

<span id="user-settings-price-preview-checkbox" class="user-settings-selector-checkbox active"></span>

and when is not selected:

<span id="user-settings-price-preview-checkbox" class="user-settings-selector-checkbox"></span>

How can I verify it using selenium and TestNG?


Solution

  • To verify if the WebElement is selected or not you can try:

        String attr = driver.findElement(By.id("user-settings-price-preview-checkbox")).getAttribute("class");
        if(attr.contains("active"))
            System.out.println("WebElement selected");
        else
            System.out.println("WebElement NOT selected");