Java Selenium - Unable to locate an element - tried all possible locators

Viewed 32

On the website/option https://demoqa.com/checkbox, I check the "Home" option. After that, in my automatic test, I would like to check, whether the "Downloads" button is checked as well. But, I keep getting no such element error. I tried xpath, full xpath, "label[for='tree-node-downloads']", even finding it as a sibiling of the element containing "Download label: "//*[text()='Downloads']/preceding-sibling::input[@type='checkbox']". With no luck.

private By checkBoxDownLoads = By.xpath("//*[text()='Downloads']/preceding-sibling::input[@type='checkbox']");
private By downArrow = By.xpath("//*[@id=\"tree-node\"]/ol/li/span/button");

public boolean example(){
       getDriver().findElement(downArrow).click();
       WebElement download = getDriver().findElement(checkBoxDownLoads);
       return download.isSelected();
    }
1 Answers

You need to get the value of class attribute to validate if the element is check or not.

If the checkbox is checked, it's class has -

class="rct-icon rct-icon-check"

and if it is not checked then it has class as

class="rct-icon rct-icon-uncheck".

You can then put an if condition to suit your needs.

The xpath for this element is -

//span[text()='Downloads']/preceding-sibling::span[1]
Related