0

I would like to select all checkboxes in a table, using javascript language in Selenium webdriver. I'm new to Selenium and I just can't find the right solution, so I would be grateful for any help.

PS. I have 10 rows in my table.

The html:

<tr role="row" class="odd">
   <td class="dt-row="0" dt-column="0"> 
     <span class="grid-select-row-span">        
       <label class="check">           
          <input type="checkbox" class="grid-select-row">
          <i></i>       
        </label>   
   </span>
</tr>
<tr role="row" class="even">
   <td data-dt-row="1" data-dt-column="0">
      <span class="grid-select-row-span">       
         <label class="check">         
            <input type="checkbox" class="grid-select-row">
               <i></i>     
         </label>   
      </span>
    </td>
 </tr> 

My selenium code:

driver.findElements(By.css("grid-select-row")).then(function (elements) { 
     elements.forEach(function(element) {
          console.log(elements.length);
         if(!element.Selected)
         {
             driver.actions() .
             mouseDown(element) .
             click(element);

            // element.click();
         }
     });              
    });
JanP
  • 1
  • 1
  • I think the function for select state is isSelected() in webdriverio. CHeck this out - http://webdriver.io/api/state/isSelected.html and for clicking the checkbox - http://webdriver.io/api/action/click.html. – Grasshopper Sep 08 '16 at 15:31
  • Do you get any errors? – Andersson Sep 08 '16 at 15:52
  • @Andersson no, I don't get any error, neither nothing will happen :/ – JanP Sep 09 '16 at 07:52
  • @Grasshopper I've already tried this: if(!element.isSelected()) { element.click(); } but I got the same result, I didn't get any errors, neither nothing happen :/ – JanP Sep 09 '16 at 08:01
  • @JanP I am not a Javascript expert but the sample code in the documentation and what you are using are different. They are passing the selector into the isSelected() method and then using promise as '.then' to work on it further. – Grasshopper Sep 09 '16 at 08:10
  • @Grasshopper Do you mean the sample code in webdriver.io (upper documentation)? I'm using selenium-webdriver not webdriverio.. – JanP Sep 09 '16 at 09:22

1 Answers1

0

To point to element with some class you should use form .className, but not just className, so try this CSS selector instead:

input.grid-select-row
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • I tried it, but it still won't check the checkboxes. – JanP Sep 09 '16 at 09:22
  • Could you try `document.querySelector("input.grid-select-row").click();` from browser console? If it works, you should check your code syntax – Andersson Sep 09 '16 at 10:04
  • I tried and it selected just the first checkbox. I also tried jQuery in browser console: $("input.grid-select-row").prop("checked", true); and it worked fine as I wanted. But I can't figured it out how to do it in selenium. – JanP Sep 09 '16 at 12:31
  • Yes, it should check just first one- I just wanted to check correctness of locator. In `selenium` it works in exactly the same way as in your `jQuery`: check current state and make click if element unchecked with appropriate methods `isChecked()` and `click()` – Andersson Sep 09 '16 at 12:46
  • so this: var enElement=driver.findElement(By.css("input.grid-select-row")); if(enElement.isSelected()) { console.log("Element je selectan"); } else { enElement.click(); } should work? – JanP Sep 21 '16 at 09:31
  • I'm not sure about syntax as I have a little expirience with `JS`. If `enElement.isSelected()` not works, try `enElement.isChecked()` – Andersson Sep 21 '16 at 10:46
  • .isChecked() is not a function, so I tried .isChecked and got an error: ElementNotVisibleError: element not visible – JanP Sep 21 '16 at 12:28
  • Seems that you should use `isDisplayed()` method in loop to wait until target checkbox to be visible as there is no such possibilities in `JS` as in other languages to use explicit wait – Andersson Sep 21 '16 at 14:30