简体   繁体   中英

How to select all checkboxes with Selenium-WebDriver with javascript

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();
         }
     });              
    });

要指向带有某些class元素,您应该使用.className形式,而不仅仅是className ,因此请尝试使用此CSS选择器:

input.grid-select-row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM