Java Selenium检查复选框标签状态

问题描述 投票:1回答:2

我正在做Selenium自动化应用程序,我不知道如何检查复选框输入状态。也许有人也有这个问题?

这是js + jquery中的工作示例

$("input:checkbox").click(function() {
    console.log($(this).is(":checked"));
    if ($(this).is(":checked")) {
        //true
    } else {
        //false
    }
});

https://jsfiddle.net/67j8xhy0/3/

java css html5 selenium input
2个回答
0
投票

如果你真的想要你可以尝试xpath

driver.findElement(By.xpath("//input[@type="checkbox")).isSelected()

但是,如果你有一个复选框的ID,你可以做

driver.findElement(By.id("checkbox-id")).isSelected()

这是doc:https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html#isSelected--


-2
投票

你可以使用下面的代码任何一个可以工作,但最好我会使用几乎总是有效的xpath:

driver.findElement(By.xpath/id/csssector("enter your data")).isSelected();
© www.soinside.com 2019 - 2024. All rights reserved.