How to check if WebElement is selected or not

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

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

and when not selected:

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

How can I test this with selenium and TestNG?

thank

+4
source share
2 answers

To check if it is selected WebElementor 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");
+1
source

Code snippet:

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

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

Assert.assertTrue(isItemSelected);
0
source

Source: https://habr.com/ru/post/1688595/


All Articles