首页 > 解决方案 > 如何单击特定的复选框

问题描述

我想点击“全部”下拉选项中的“选择页面”复选框。

html:

<ul class="list-unstyled">
    <li>
        <input id="selectAllTop" name="selectAllCheckBox" attr-sel="All" type="checkbox" value="true" /><input type="hidden" name="_selectAllCheckBox" value="on" />
        <label for="selectAllTop" class="checkbox-label" title="Select all results">Select all</label>
    </li>
    <li>
        <input id="selectPageTop" name="selectPageCheckBox" attr-sel="Page" type="checkbox" value="true" /><input type="hidden" name="_selectPageCheckBox" value="on" />
        <label for="selectPageTop" class="checkbox-label" title="Select page results">Select page</label>
    </li>
</ul>

我收到此错误:

ElementNotVisibleException: Message: element not visible

我已成功选择“全部”按钮,但复选框有问题,这是我所做的代码,但它不起作用。

button=driver.find_element_by_xpath("//div[@id='selectAllOrPage']//button[@type='button']")
button.click()

time.sleep(5)
checkboxes = driver.find_element_by_xpath("//input[@id='selectPageTop' and @type='checkbox']")
checkboxes.click()

任何类型的帮助都将是可观的。

谢谢

标签: python-3.xseleniumselenium-webdriverxpathcss-selectors

解决方案


根据您共享的HTML<input> ,该标签有一个兄弟<label>标签。因此,要单击复选框选择页面,您可以使用以下代码行:

  • css_selector

    driver.find_element_by_css_selector("ul.list-unstyled li label[for=selectPageTop]").click()
    
  • 路径

    driver.find_element_by_xpath("//ul[@class='list-unstyled']//li//label[@for='selectPageTop']").click()
    

推荐阅读