0
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我一个错误,上面写着

findElements(By) 方法不适用于参数 (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

    }

.getText();但是现在的新错误是当我尝试followButton

String followOrNot = followButton.getText();

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

followButton 无法解决

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

任何人都可以帮忙吗?

4

1 回答 1

2

你可以试试这个

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或者class name你也可以使用下面的代码

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

更多细节在这里

于 2020-01-15T17:26:44.637 回答