java - findElements(By) 方法不适用于参数 (MobileElement)

标签 java android mobile find appium

MobileElement followButton = (MobileElement) driver.findElement(By.xpath("//android.widget.ListView[@index='0']//android.widget.FrameLayout[@index='"+loopVal+"']//android.widget.LinearLayout[@index='0']//android.widget.FrameLayout[@index='2']//android.widget.TextView[@index='0']"));

    if(driver.findElements(followButton).isEmpty()) {

    //do something

    }

findElements它给了我一个错误,上面写着

The method findElements(By) is not applicable for the arguments (MobileElement)

所以我尝试将这个方法重新组织为 try-catch

try {

    MobileElement followButton = (MobileElement) driver.findElement(By.xpath("//android.widget.ListView[@index='0']//android.widget.FrameLayout[@index='"+loopVal+"']//android.widget.LinearLayout[@index='0']//android.widget.FrameLayout[@index='2']//android.widget.TextView[@index='0']"));

    } catch (org.openqa.selenium.NoSuchElementException e) {

        //do something

    }

但是现在新的错误是当我尝试 followButton

.getText();
String followOrNot = followButton.getText();

它给了我一个错误,上面写着

followButton cannot be resolved

基本上我想做的是找到 followButton 并运行 .getText() 但如果找不到 followButton ,则执行操作处理错误

有人可以帮忙吗?

最佳答案

你可以试试这个

List<AndroidElement> followButtons = driver.findElements(MobileBy.xpath("//android.widget.ListView[@index='0']//android.widget.FrameLayout[@index='"+loopVal+"']//android.widget.LinearLayout[@index='0']//android.widget.FrameLayout[@index='2']//android.widget.TextView[@index='0']"));


if(! followButtons.isEmpty()) {

    System.out.println(followButtons.get(0).getText()); // print text

    }

如果您有id类名,您也可以使用下面的代码

List<MobileElement> elementsOne = (List<MobileElement>) driver.findElementsByAccessibilityId("SomeAccessibilityID");
List<MobileElement> elementsTwo = (List<MobileElement>) driver.findElementsByClassName("SomeClassName");

更多详细信息是here

关于java - findElements(By) 方法不适用于参数 (MobileElement),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59756139/

相关文章:

Java Sax 解析器 - 在 EndElementListener 中停止处理

java - 将 EventSource(服务器发送的事件)连接到服务器时出错

java - java中如何分割两行?当我尝试按/n 分割时,该字符串正在打印垃圾值 :(

java - 许可证/ key java 应用程序的解决方案

android - 解锁设备时 onConfigurationChanged 不是第一次调用

android - 连接到 connect() 处的 url 时出现 IO 异常

css - Bootstrap 固定 header 在 iPad 上不起作用

android - 读取音频文件以将值保存在Android中的文本文件中

javascript - 防止页面滚动

ios - PhoneGap 和 iPhone 5 : Remove top and bottom black stripes