Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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.

share|improve this question
    
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 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 at 17:06

2 Answers 2

up vote 2 down vote accepted
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.

share|improve this answer
    
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 at 13:33
    
So you are only attempting to click one of them randomly? –  PaulDonny Apr 4 at 13:35
    
yes you're correct im clicking one at random –  Emmanuel Angelo.R Apr 4 at 13:36
    
Thanks paul it gives me the answer to it. –  Emmanuel Angelo.R Apr 5 at 6:16

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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