Selenium Python按输入类型选择并单击多个复选框元素

hgtggwj0  于 2023-02-08  发布在  Python
关注(0)|答案(2)|浏览(201)

尝试选择并点击一个页面上的多个复选框。它们没有匹配的类、ID或名称。它们的共同点是它们的输入类型(复选框)。我可以使用XPATH选择单个复选框,但它不实用,因为页面上至少有100个复选框。
这是HTML的一部分,我已经在这个网站的离线版本上测试了这段代码。

<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>
  • 尝试1 -选择并单击第一个复选框,但不选择并单击其他复选框
checkboxes = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']").click()
  • 尝试2 -以为这会工作,但我不能得到语法的权利
checkboxes = driver.find_element(By.CSS_SELECTOR, "input[type='checkbox']")
    for checkbox in checkboxes:
    checkboxes.click()
    time.sleep(.3)
  • 尝试3 -能够选择具有此名称的第一个复选框(共3个)
checkboxes = driver.find_element("name", "competency1Ativity").click()
qyyhg6bp

qyyhg6bp1#

find_element()将只返回第一个匹配的元素,而您需要标识所有这些元素。因此,您需要使用find_elements()方法。
溶液
您可以标识所有checkbox并将元素存储在 list 中,然后使用以下locator strategies之一逐个单击它们:

  • 使用 *CSS选择器 *:
from selenium.webdriver.common.by import By
checkboxes = driver.find_elements(By.CSS_SELECTOR, "td input[type='checkbox']")
for checkbox in checkboxes:
    checkbox.click()
  • 使用 XPATH
from selenium.webdriver.common.by import By
checkboxes = driver.find_elements(By.XPATH, "//td//input[@type='checkbox']")
for checkbox in checkboxes:
    checkbox.click()
a8jjtwal

a8jjtwal2#

您的option 2几乎是正确的,但是它应该是find_elements()而不是find_element(),因为find_element()只返回一个webelement,而find_elements()返回元素列表。

    • 代码:**
checkboxes = driver.find_elements(By.CSS_SELECTOR, "input[type='checkbox']")
for checkbox in checkboxes:
  checkbox.click()
  time.sleep(0.5)

相关问题