复选框的Selenium Isselected()方法始终返回false for Android



我需要测试的Android应用中的复选框列表。因此,当我使用Isselected()方法时,它总是返回false,无论是否检查此复选框。正在尝试使用FindElementById()和ByxPath()。相同的结果。

这是我使用的代码部分:

WebElement checkBox = driver.findElementById(appType + id);
    if (!checkBox.isSelected()){
        Reporter.log(backupDataType + " checkbox isn't checked. clicking on it...", true);
        ...}

使用XPath:

checkBox = driver.findElementByXPath("//android.widget.CheckBox[@resource-id='" + appType + checkSms + "']");
    if (!driver.findElementByXPath("//android.widget.CheckBox[@resource-id='" + appType + checkSms + "']").isSelected()){
    Reporter.log(backupDataType + " checkbox isn't checked. clicking on it...", true);
    ...}

通往元素的路径是正确的,因为它总是单击它。没关系,是否检查过。

考虑到您正在使用Android复选框窗口小部件,您应尝试使用getAttribute的 CC_1属性,如下所示 -

WebElement element = <find element by your locator strategy>; // in your case  driver.findElementByXPath("//android.widget.CheckBox[@resource-id='" + appType + checkSms + "']");
String checkboxAttribute = element.getAttribute("checked");
if(!checkboxAttribute.equalsIgnoreCase("true")) {
   Reporter.log(backupDataType + " checkbox isn't checked. clicking on it...", true);
   ...
}

P.S - 尝试练习保存WebElement而不是再次找到它。

最新更新