python - 使用 Python 和 Selenuim 选择复选框
问题描述
如果有人知道如何使用 Selenium 和 Python 选择复选框,那就太好了。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
no_class = browser.find_element(By.XPATH, "//div[@id='icon-literary-collections']/following-sibling::a[1]")
no_class.click
这是 HTML 部分:
<div class="a-checkbox" style="">
<label for="checkbox-non--classifiable" style="">
<input id="checkbox-non--classifiable" type="checkbox" name="" value="" nodeid="non--classifiable" style="" class="">
<i class="a-icon a-icon-checkbox"></i>
<span class="a-label a-checkbox-label" style="">Non-Classifiable</span>
</label>
</div>
我想选中“不可分类”复选框,但我无法做到这一点我尝试了 id、name、link_text 但无法检测到应该使用什么?
解决方案
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[@class='a-label a-checkbox-label']"))).click()
Javascript 点击:
checkBox=WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[@class='a-label a-checkbox-label']")))
driver.execute_script("arguments[0].click();", checkBox)
注意:请在您的解决方案中添加以下导入
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
推荐阅读
- c - 有没有什么情况下 IO 函数永远不会失败?
- kubernetes - kubectl 错误:无法添加密钥 dashboard.yaml,该名称的另一个密钥已存在
- 3d - 根据其在 Godot 中的旋转变换空间节点
- java - 如何在 GSON 反序列化期间忽略空字符串
- r - R Shiny中的相互依赖的过滤器级联 - updatePickerInput
- java - 如何使用 Room 创建多个表?
- swift - 进入后台后如何保持在打开的 WKWebView 上创建的 WebSocket 连接一段时间
- django - 如何在 Django 模板的内循环部分访问外循环上下文值
- c# - WCF 服务 SSRS 在我的浏览器上通过 URL 工作,但在 WCF 客户端上不工作?
- r - 尝试迭代函数的输出以进行引导