5

I've got this HTML code

<input type="checkbox" value="abc" name="arr[]><br>
<input type="checkbox" value="abc1" name="arr[]><br>
<input type="checkbox" value="abc2" name="arr[]><br>

With this

self.browser.find_element_by_xpath("//input[@name='arr[]']").click()

I can check only the first checkbox, but I want check all check box in the same time. How I can do it?

  • You probably can't check all the checkboxes at the same time (actually simultaneously). That's an action which would be impossible for a normal user, so selenium is unlikely to provide a way to do so. – Vince Bowdren Aug 6 '13 at 8:54
8
checkboxes = self.browser.find_elements_by_xpath("//input[@name='arr[]']")
for checkbox in checkboxes:
    if not checkbox.isSelected():
        checkbox.click()

(Previous answer):

I am not yet familiar with the python syntax, but this is what you can do:

  1. Return all elements with the given xpath:

    self.browser.find_elements_by_xpath("//input[@name='arr[]']")
    Note that it is find_elements_by_xpath (plural)

  2. Loop through the list to check all checkbox

0

Try changing your Xpath to:

//input[@name='arr[]'][2]
0

In Java that would be:

WebElement box = driver.findElement(By.xpath(".//*[@id='multi-selections']"));
List<WebElement> lc = box.findElements(By.tagName("input"));
for (int i = 0; i <= lc.size(); i++) {
  lc.get(i).click();
}
  • Please, add an information you're answer is in Java, while question was about Python – dzieciou Nov 1 '12 at 17:04
0

In selenium IDE there is a command named check and un check so you can use the command for your testing case if needed.

<tr>
<td>check</td>
<td>loggerCheck</td>
<td></td>

<tr>
    <td>uncheck</td>
    <td>loggerCheck</td>
    <td></td>
</tr>
0

Find element (singular returns only the first element that matches), whereas find_elements returns an iterable list of all matches. As Suchit explained, you can loop through the list.

You can also select an individual box in the list using the index. For example, to select the second box: driver.find_elements_by_xpath("//input[@name='arr[]']")[1].click()

0

Suchit Parikh's Answer is close, but when selenium tries to click the second checkbox, it is likely that it will throw a StaleElementReferenceException

You can get by this by storing some unique information from the check boxes in another list, then iterate through that new list and finding the elements again.

Example:

   elements = self.driver.find_elements_by_xpath("//input[@name='arr[]']")

   element_list = []
   for elm in elements:
       element_list.append(elm.get_attribute('value'))

   for elem_value in element_list:
       self.driver.find_element_by_xpath("//input[@value='" + elem_value + "']").click()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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