0

I have a method:

     public void clickCheckBox(int index) {
       List<WebElement> sizeList = getSizeList();
       WebElement checkbox = sizeList.get(index);
       Actions action = new Actions(driver);
       action.moveToElement(checkbox).click().build().perform();
 }

This method is taking the index of a list of checkboxes and then using Actions class to check the checkbox. I am doing it this way because the page I am currently working with triggers an event when this checkbox is checked so just doing a simple find the checkbox and click doesn't work (event doesn't fire).

My above code is not checking the checkbox and I'm not sure why. Any insight would be great. Please let me know if I didn't provide enough information.

EDIT:

Here is my method getting the size of the list:

     public List<WebElement> getSizeList(){
         List<WebElement> sizeList = body.findElement(By.cssSelector("ul")).findElements(By.cssSelector("li"));
         if(null==sizeList) {
           // oops, couldn't find the element
           LOGGER.error("Failed to locate the 'li' element for the action button");
           return Collections.emptyList();
         }
         return sizeList;
 } 

Here is a bit of the HTML:

  <ul>          
  <li>
    <input type="checkbox">
    <a href="www.url.com" title="8">8</a>
<span class="count">(1,037)</span>
</li>


  <li>
    <input type="checkbox">
    <a href="www.url.com" title="10">10</a>
<span class="count">(1,047)</span>
</li> ...
2
  • 1
    Adding the HTML for the checklist, and the code that gets the checklist would help us to help you. One thing you can try is printing out the size of the list, to ensure it's greater than 0. Commented Feb 20, 2014 at 16:55
  • Edited my question with those requests @Richard Commented Feb 20, 2014 at 17:04

1 Answer 1

1

Your finding the element up to li but not input (Checkbox)

//get the checkboxes size with below command

List<WebElement> sizeList = body.findElements(By.cssSelector("ul>li>input"));

or

List<WebElement> sizeList = body.findElements(By.cssSelector("input[type='checkbox']"));

Now do your operations with above list.

for(WebElement eachChkBox : sizeList) {
     new Actions(driver).click(eachChkBox).perform(); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.