0

Trying to select and click multiple checkboxes on a page. they don't have a class, ID or names that match. All they have in common is their input type (checkbox). I'm able to select individual checkboxes using XPATH but its not practical as there's at least 100 checkboxes on the page.

Here's a section of the HTML. I've been testing this code on an off-line version of the site.

<body>
    <tbody>
      <tr>
        <td class="borderBot"><b>Activity</b></td>
        <td class="borderBot">
          <table cellpadding="3" cellspacing="3" width="100%">
            <tbody>
              <tr>
                <td width="33%">
                  <input type="checkbox" name="competency1Activity" value="1" />
                  Plan (ie Interpreted diag etc)
                </td>

                <td width="33%">
                  <input type="checkbox" name="competency1Activity" value="2" />
                  Carry Out (ie conducted work)
                </td>

                <td width="33%">
                  <input type="checkbox" name="competency1Activity" value="4" />
                  Complete (ie Compliance etc)
                </td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>

      <tr>
        <td class="borderBot"><b>Supervision</b></td>
        <td class="borderBot">
          <table cellpadding="3" cellspacing="3" width="100%">
            <tbody>
              <tr>
                <td width="33%">
                  <input
                    type="checkbox"
                    name="competency1Supervision"
                    value="1"
                  />
                  Direct
                </td>

                <td width="33%">
                  <input
                    type="checkbox"
                    name="competency1Supervision"
                    value="2"
                  />
                  General
                </td>

                <td width="33%">
                  <input
                    type="checkbox"
                    name="competency1Supervision"
                    value="4"
                  />
                  Broad
                </td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>

      <tr>
        <td class="borderBot"><b>Support</b></td>
        <td class="borderBot">
          <table cellpadding="3" cellspacing="3" width="100%">
            <tbody>
              <tr>
                <td width="33%">
                  <input type="checkbox" name="competency1Support" value="1" />
                  Constant
                </td>

                <td width="33%">
                  <input type="checkbox" name="competency1Support" value="2" />
                  Intermittent
                </td>

                <td width="33%">
                  <input type="checkbox" name="competency1Support" value="4" />
                  Minimal
                </td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>

      <tr>
        <td class="borderBot"><b>Materials</b></td>
        <td class="borderBot">
          <table cellpadding="3" cellspacing="3" width="100%">
            <tbody>
              <tr>
                <td width="50%">
                  <input type="checkbox" name="competency1Extended" value="1" />
                  Insulation failure
                </td>

                <td width="50%">
                  <input type="checkbox" name="competency1Extended" value="2" />
                  Incorrect connections
                </td>
              </tr>

              <tr>
                <td width="50%">
                  <input type="checkbox" name="competency1Extended" value="4" />
                  Circuits-wiring; eg. open short
                </td>

                <td width="50%">
                  <input type="checkbox" name="competency1Extended" value="8" />
                  Unsafe condition
                </td>
              </tr>

              <tr>
                <td width="50%">
                  <input
                    type="checkbox"
                    name="competency1Extended"
                    value="16"
                  />
                  Apparatus/component failure
                </td>

                <td width="50%">
                  <input
                    type="checkbox"
                    name="competency1Extended"
                    value="32"
                  />
                  Related mechanical failure
                </td>
              </tr>

              <tr>
                <td width="50%">
                  <input
                    type="checkbox"
                    name="competency1Extended"
                    value="64"
                  />
                  Read/interpret drawings/plans
                </td>

                <td width="50%">
                  <input
                    type="checkbox"
                    name="competency1Extended"
                    value="128"
                  />
                  Other elec app and circuit faults
                </td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>
      <input
        type="hidden"
        id="competency1ExtendedCount"
        name="competency1ExtendedCount"
        value="8"
      />
    </tbody>
  </body>
  • Try 1 - this selects and clicks the first checkbox but none of the others
checkboxes = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']").click()
  • Try 2 - Thought this would work but I cant get the syntax right
checkboxes = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']")
    for checkbox in checkboxes:
    checkboxes.click()
    time.sleep(.3)
  • Try 3 - able to select first checkbox (of 3) with this name
checkboxes = driver.find_element("name", "competency1Ativity").click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Mitch
  • 3
  • 2

2 Answers2

0

find_element() will return only the first matching element, where as you need to identify all of them. So you need to use find_elements() method.


Solution

You can identify all the and store the elements within a list and then click on them one by one using either of the following locator strategies:

  • Using CSS_SELECTOR:

    from selenium.webdriver.common.by import By
    checkboxes = driver.find_elements(By.CSS_SELECTOR, "td input[type='checkbox']")
    for checkbox in checkboxes:
        checkbox.click()
    
  • Using XPATH:

    from selenium.webdriver.common.by import By
    checkboxes = driver.find_elements(By.XPATH, "//td//input[@type='checkbox']")
    for checkbox in checkboxes:
        checkbox.click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Your option 2 was nearly correct, however it should have been find_elements() not find_element() Since find_element() just returns an webelement where as find_elements() returns list of elements.

code:

checkboxes = driver.find_elements(By.CSS_SELECTOR, "input[type='checkbox']")
for checkbox in checkboxes:
  checkbox.click()
  time.sleep(0.5)
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • I get this error with that code: AttributeError: 'list' object has no attribute 'click' – Mitch Feb 07 '23 at 09:54
  • @Mitch, you need to copy and paste exact my code. it should work fine. I am sure you might misjudge the variable. instead of `checkbox.clcik()` you might used `checkboxes.click()` Please check once again. – KunduK Feb 07 '23 at 10:17