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/