如何使用 Selenium webdriver 获取该复选框是启用还是禁用状态?

How to get that check box is enable or disable status using Selenium webdriver?

html代码:

input type="checkbox" class="checkbox_one" disabled

如果复选框未禁用,文本不会出现。

Assert.assertTrue(webelemt.getAttribute("innerHTML").contains("disabled"));

您可以使用....

ele = driver.find_element_by_class_name('checkbox_one')

if ele.is_selected():
    // do something
else:
    // do something

您可以调用 .getAttribute 方法来设置一个条件,在该条件下它将检查其内容,如果不为空,则断言应该通过,如果为空,则断言应该失败。

代码:

    WebElement webelemt = driver.findElement(By.xpath("//span[@title='fieldItem']//preceding-sibling::input"));
    String checkBoxStatus = "";
    if (!webelemt.getAttribute("disabled").isEmpty() && webelemt.getAttribute("disabled").contains("disabled")) {
        System.out.println("Disabled must be present");
        checkBoxStatus = "disabled";
        Assert.assertTrue(true);
    }
    else {
        System.out.println("Should not it be enabled  ?  or do we need if-else to handle that part as well.");
        Assert.assertTrue(false);
    }