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.

  • 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 Apr 4 '14 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 Apr 4 '14 at 17:06
int i = 0;
for (WebElement we:driver.findElements(By.name("vehicle")) {
    if (we.isSelected()) { 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.

  • 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'. – Emmanuel Angelo.R Apr 4 '14 at 13:33
  • So you are only attempting to click one of them randomly? – Paul Muir Apr 4 '14 at 13:35
  • yes you're correct im clicking one at random – Emmanuel Angelo.R Apr 4 '14 at 13:36

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, privacy policy and cookie policy

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