6

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?

dzieciou
10.5k9 gold badges49 silver badges102 bronze badges
asked Jun 17, 2012 at 21:54
1
  • 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. Commented Aug 6, 2013 at 8:54

6 Answers 6

9
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

answered Oct 1, 2012 at 17:00
0

Try changing your Xpath to:

//input[@name='arr[]'][2]
answered Jun 18, 2012 at 12:56
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();
}
dzieciou
10.5k9 gold badges49 silver badges102 bronze badges
answered Jul 3, 2012 at 13:03
1
  • Please, add an information you're answer is in Java, while question was about Python Commented Nov 1, 2012 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>
answered Feb 19, 2014 at 5:02
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()

answered Nov 13, 2014 at 21:34
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()
answered Mar 19, 2018 at 19:17

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.