python-3.x - 如何单击特定的复选框
问题描述
我想点击“全部”下拉选项中的“选择页面”复选框。
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()
任何类型的帮助都将是可观的。
谢谢
解决方案
根据您共享的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()
推荐阅读
- c - Cortex M3、STM32、thumb2:我的 inc 和 dec 操作不是原子的,但应该是原子的。这里有什么问题?
- javascript - “连接”上的 Socket.io 服务器未触发,客户端事件未触发也是如此
- reactjs - 反应任何道具解构
- runit - 没有暂停的runit启动脚本
- python - Pydantic cpecify 配置文件
- python - 如何根据长度和字母对数组进行排序,其中字母优先于长度python
- python - Python中If Else逻辑的字符串连接问题
- flutter - Flutter customPaint 状态
- python - 与其他数据类型一起插入 one-hot
- css - SVG不渲染
当添加 css all:revert