如何知道是否使用java在selenium中选中了复选框

时间:2016-06-29 06:06:27

标签: java selenium

我试图获取复选框的状态,无论是否使用java在selenium中检查。 下面是html的代码和我编写的代码:

HTML代码::

<div class="icheckbox_square-aero" style="position: relative;" aria-checked="false" aria-disabled="false">

<input id="IAgree" class="icheckbox_square-aero" type="checkbox" required="" name="IAgree" style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;">

<ins class="iCheck-helper" style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"></ins>
</div>

编程代码::

String loc_iAgree  = pr.getProperty("iagree_xpath"); 
//iagree_xpath is declared in properties file as "iagree_xpath = //form[@id='myWizard']/div/section[3]/div[3]/div/ins"
uia.clickOnCheckbox("xpath", loc_iAgree);

clickOnCheckbox()方法的代码::

    public void clickOnCheckbox(String loc_Type, String loc_Value) {
    try {
        if (loc_Type.equalsIgnoreCase("id")) {
            element = driver.findElement(By.id(loc_Value));
        } else if (loc_Type.equalsIgnoreCase("name")) {
            element = driver.findElement(By.name(loc_Value));
        } else if (loc_Type.equalsIgnoreCase("xpath")) {
            element = driver.findElement(By.xpath(loc_Value));
        } else if (loc_Type.equalsIgnoreCase("linktext")) {
            element = driver.findElement(By.linkText(loc_Value));
        } else if (loc_Type.equalsIgnoreCase("partiallinktext")) {
            element = driver.findElement(By.partialLinkText(loc_Value));
        } else if (loc_Type.equalsIgnoreCase("cssSelector")) {
            element = driver.findElement(By.cssSelector(loc_Value));
        } else {
            System.out.println("provide valid locator Type");
        }

        String value;
        if (element.isEnabled()) {
            element.click();
            value = element.getAttribute("checked");
            System.out.println(value);
        } else {
            System.out.println("element is not present or not enabled");
        }
    } catch (Exception e) {
        throw (e);
    }
}

此处,即使选中复选框,该值也会返回null。

有没有其他方法可以将复选框的状态恢复到调用程序?

1 个答案:

答案 0 :(得分:0)

Selenium WebDriver中的isSelected() Link