使用python和selenium单击复选框元素

使用python和selenium单击复选框元素,python,selenium,ui-automation,Python,Selenium,Ui Automation,我正在尝试自动加载和配置网页。 在这个页面上,我试图检查复选框的状态,然后单击一个复选框,如果它没有选中 HTML页面代码: <fieldset id="wireless-24" class="left" style="display: block;"> <div class="legend-edit text-orphan"> <legend>2.4 GHz network</le

我正在尝试自动加载和配置网页。 在这个页面上,我试图检查复选框的状态,然后单击一个复选框,如果它没有选中

HTML页面代码:

<fieldset id="wireless-24" class="left" style="display: block;">
                <div class="legend-edit text-orphan">
                    <legend>2.4 GHz network</legend><span>|</span><span id="edit-wireless-24"><a id="editWireless24Link" href="http://192.168.0.1/ui/1.0.99.162351/dynamic/home.html#">Edit</a></span>
                </div>
                <div class="row toggle-edit">
                    <label style="width: 126px;">Network:</label>
                    <label id="w-radio-enabled-text-24">Disabled</label> <!-- code sets the text to either Enabled or Disabled -->
                    <div class="cell" id="w-enabled-span-24">
                        <div class="check-item" style="">
                            <div><input type="checkbox" id="w-enabled-24" name="radio.band24.settings.isEnabled" class="ui-helper-hidden-accessible"><span class="ui-checkbox"></span></div>
                            <div><label for="w-enabled-24" class="ui-checkbox">Enabled</label></div>
                        </div>
                    </div>
                </div>

首先,您试图
将密钥
发送到
标签
标签,有什么问题吗。您可以
send_key
input
标签或同等物品。其次,您试图点击
标签
,从我的角度来看,这也没有效果。我相信你想要这样的东西:

checkbox = driver.find_element_by_id('w-enabled-24') # locate checkbox
if not checkbox.is_selected(): # check if checkbox is already selected
    checkbox.click() # if not, click on it
checkbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "w-enabled-24")))
checkbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "w-enabled-24")))
if not checkbox.is_selected():
    checkbox.click()
此外,我还要添加
WebDriverWait
,以确保该元素是可见的,并且可以像这样单击:

checkbox = driver.find_element_by_id('w-enabled-24') # locate checkbox
if not checkbox.is_selected(): # check if checkbox is already selected
    checkbox.click() # if not, click on it
checkbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "w-enabled-24")))
checkbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "w-enabled-24")))
if not checkbox.is_selected():
    checkbox.click()
这将至少等待10秒钟,直到复选框可单击为止。完整代码如下所示:

checkbox = driver.find_element_by_id('w-enabled-24') # locate checkbox
if not checkbox.is_selected(): # check if checkbox is already selected
    checkbox.click() # if not, click on it
checkbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "w-enabled-24")))
checkbox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "w-enabled-24")))
if not checkbox.is_selected():
    checkbox.click()
注意:您必须添加一些导入:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By

有关
WebDriverWait
的更多信息,您将找到。

当您
向单选按钮发送密钥(“已启用”)
时会发生什么情况?@Klot您没有提到,错误详细信息。请这样做。