1

As there are five number of check boxes in a page, i have selected two check boxes. so i want to get the number of size as two check box selected. How to get the number of size as two in selenium web-driver?

Note: List<WebElement> boxes = driver.findElements(By.name("vehicle")).click(); System.out.println(boxes.size() + "Number of check boxes present in the page");

it gives me the entire number of check boxes, As the above scenario is not needed.

2
  • It is not clear to me what you are trying to do here. You are both getting a list of the elements with that name and clicking on them?
    – Dan Snell
    Commented Apr 4, 2014 at 16:20
  • @Emmanuel Angelo- I used PoulDonny code in my TEST APPLICATION and was working fine. It is working as you mentioned in your scenario :). I was selected 2 check-boxes and O/P: 2 Number of check boxe is selected in the page. Please check your query and discussions?. YOU NEED TO POST CRISTAL CLEAR QUERY..
    – QA4it
    Commented Apr 4, 2014 at 17:06

2 Answers 2

3
driver.findElements(By.name("vehicle").click();
int i = 0;
for (WebElement we:driver.findElements(By.name("vehicle")) {
    if (we.isSelected()) { i++; }
}
System.out.println(i
            + " Number of check boxes present in the page");

That should give no issues. But you are looping through an entire list to always come to the same answer, 1.

3
  • as there are three check-boxes in the web page, i have clicked only one box. so at this point i should the get the system.out.println as '1 check box selected'. All the check-box have the same element name 'vehicle'. Commented Apr 4, 2014 at 13:33
  • So you are only attempting to click one of them randomly?
    – Paul Muir
    Commented Apr 4, 2014 at 13:35
  • yes you're correct im clicking one at random Commented Apr 4, 2014 at 13:36
2

I'm no wizard when it comes to actually coding this, but I used to inject variables with JS into Selenium IDE (back in the day) that would collect certain actions a they were performed. Perhaps you can use the action for clicking the checkbox to increase a simple count variable to use in your script above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.