首页 > 解决方案 > 如何检查复选框是否已在编辑页面上选中

问题描述

根据场景,如果复选框已经被选中,那么我只需要打印文本,如果复选框没有被选中,那么我需要点击复选框。

    //-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();
    }  

HTML 标记是

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

谁能帮我?如何在执行时检查此复选框是否已选中?我共享了 DOM 元素。

标签: javaselenium-webdriver

解决方案


isSelected()不适用于LABEL标签,仅适用于INPUTs。在INPUT您发布的 HTML 中。

boolean sandboxClientCheckbox = driver.findElement(By.id("acChkSandboxClient")).isSelected();

推荐阅读