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.

asked Apr 4, 2014 at 7:52
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? 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.. 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.

answered Apr 4, 2014 at 13:29
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? 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?

answered Apr 4, 2014 at 10:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.