首页 > 解决方案 > 使用 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 但无法检测到应该使用什么?

标签: pythonseleniumselenium-webdriver

解决方案


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

推荐阅读