如何检查复选框是否已经检查过

时间:2018-05-07 09:51:11

标签: java selenium selenium-webdriver webdriver

根据方案,如果已选中复选框,则我只需要打印文本,如果未选中复选框,则需要单击复选框。

//-Check sandbox test client checkbox is already checked or not if not then tick on checkbox
boolean sandboxClientCheckbox = driver.findElement(By.xpath("//div[@class='eplChkBoxWrapper']//label[@id='acChkSandboxClientForLbl']")).isSelected();       
System.out.println("Check checkbox value " +sandboxClientCheckbox);
if(sandboxClientCheckbox == true)   {
    Utils.pauseTestExecution(3);
    System.out.println("Checkbox is already checked  ");            
} else {
            driver.findElement(By.id("acChkSandboxClientForLbl")).click();
}  

当我打印复选框的值时,这显示为false。但第一次总是检查

复选框的HTML值为:

<div class="eplChkBoxWrapper">==$0
 <input type="checkbox" id="acChkSandboxClient" class="eplChkBox" checked>
<label id="acChkSandboxClientForLbl" for="acChkSandboxClient">
::after
</label>

在执行时,任何人都可以帮助我检查此复选框是否已经检查过。我已经分享了DOM元素enter code here

6 个答案:

答案 0 :(得分:0)

if($("#check").prop('checked') == true){
do something
}

答案 1 :(得分:0)

验证是否已选中所需复选框并仅打印文本,否则如果未选中复选框,则单击复选框可以使用以下代码块:

if(driver.findElements(By.xpath("//div[@class='eplChkBoxWrapper']/input[@checked]")).size() != 0)
{
    System.out.println("Checkbox is already checked");
    // do any other work
}
else
{
    driver.findElement(By.xpath("//div[@class='eplChkBoxWrapper']/label[@for='acChkSandboxClient']")).click();
    // do any other work
}

答案 2 :(得分:0)

webelement接口中存在的一个方法我们可以使用它来检查。方法名称是isSelected()。它的返回类型是boolean.if它是真的而不是它被选中。如果它是假的,我们可以通过click()来检查。

答案 3 :(得分:0)

查找元素:

WebElement chk = driver.findElement(By.id("chkElement1"))

所以这段代码涵盖了所有可能性,并检查是否已经检查过。

public void checkItem(boolean flgON) {
        boolean flgState = Boolean.valueOf(chk.getAttribute("checked"));

// if state is checked and want to un-check (flg == false)//
        if (flgState && (!flgON)) {
            switchSystemNotifications.click();
        }
// if state is notchecked and want check (flg == true)//
        if (!flgState && (flgON)) {
            switchSystemNotifications.click();
        }
}

答案 4 :(得分:0)

我很久以前就遇到过类似的问题了,我使用了这段代码,只是为了“修补”我当前的问题,尝试一下,这不是优雅的解决方案,但是请捏一下检查出来:

String selectedCB = "";
String jsScript = "var selchbox = [];"
    +"var inpfields = document.getElementsByName('city[]');"
    +"var nr_inpfields = inpfields.length;"
    +"for(var i=0; i<nr_inpfields; i++) {"
    +"    if(inpfields[i].type == 'checkbox' && inpfields[i].checked == true) selchbox.push(inpfields[i].value);"
    +"  }"
    +"var selected;"
    +"selected = selchbox.join('#') + \"#\";"
    +"return selected;";
JavascriptExecutor executor = (JavascriptExecutor)driver;
selectedCB = (String) executor.executeScript(jsScript);
String[] selectedB = selectedCB.split("#");

答案 5 :(得分:0)

你试过BaseVerification verification = (BaseVerification) JWT.require(algorithm) .acceptLeeway(1) .acceptExpiresAt(5); Clock clock = new CustomClock(); // Must implement Clock interface JWTVerifier verifier = verification.build(clock); 吗?如果是这样,尝试使用element.isEnabled()函数检查属性。我认为当取消选中复选框时它会消失。

相关问题