【问题标题】:how to know if a checkbox is checked in selenium using java [duplicate]如何知道是否使用java在selenium中选中了一个复选框[重复]
【发布时间】:2016-10-31 16:28:02
【问题描述】:

我正在尝试使用 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。

还有其他方法可以将复选框的状态返回给调用程序吗?

【问题讨论】:

标签: java selenium


【解决方案1】:

在 Selenium WebDriver 中的 isSelected() Link

【讨论】:

    猜你喜欢
    • 2013-10-18
    • 2012-01-01
    • 2015-11-30
    • 2013-02-07
    • 2017-09-14
    • 1970-01-01
    • 2018-07-20
    • 2013-12-15
    相关资源
    最近更新 更多