Артём Орёл
Артём Орёл

Reputation: 135

how to check if checkbox checked using Selenium

I have such element as span, which looks like checkbox. The main check box is hidden and is not displayed. In Developer tool of Edge the checkbox looks like: checkBox

In Developer tool of Edge the element looks like: In Developer tool of Edge it looks like:

on a web page this element unchecked looks:

element on a web page unchecked

and checked :

enter image description here

How can I check if the checkbox checked using Java Selenium. I need to make some tests of these checkboxes using Selenium. But when I tried to use checkbox.isChecked() it didn't checked, because it is not a check box. Then I tried to use Developer Tools of browser and found that nothing changing when I check\uncheck the checkbox. the checkbox itself is not displayed.

Upvotes: 0

Views: 177

Answers (1)

Артём Орёл
Артём Орёл

Reputation: 135

May be it will help somebody. Here is how I checked the checkbox :

JavascriptExecutor js = (JavascriptExecutor) driver;
String clickCheckBox1 = "let elem = document.evaluate(\"//*[@id='tab-1']/div[5]/label[1]/span\",document, null,XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;elem.click();";
js.executeScript(clickCheckBox1);
Helpers.wait(1);
String clickCheckBox2 = "let elem2 = document.evaluate(\"//*[@id='tab-1']/div[5]/label[2]/span\",document, null,XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;elem2.click();";
js.executeScript(clickCheckBox2);

Helpers.wait(1); is the custom class, which makes just Thread.sleep(ms) :

public static void wait(int seconds){
        try{
            int i = seconds * 1000;
            Thread.sleep(i);
        }catch (InterruptedException e){
            throw new RuntimeException(e);
        }
    }

Upvotes: 0

Related Questions