Radek Jackowiak
Radek Jackowiak

Reputation: 67

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?

Upvotes: 4

Views: 3768

Answers (2)

optimistic_creeper
optimistic_creeper

Reputation: 2799

Code snippet:

String classAttribute = driver.findElement(By.id("user-settings-price-preview-checkbox")).getAttribute("class");

boolean isItemSelected = classAttribute.endsWith("active");

Assert.assertTrue(isItemSelected);

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193188

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

Upvotes: 1

Related Questions