前置条件及环境如下

需求如下

现在我需要判断一个 button 的状态,这个 button 的状态是

我在判断的时候代码如下

//判断menubutton是不是打开状态
if (!au.isElementPresented(By.xpath("//android.view.ViewGroup/android.widget.ImageButton"))){
    //去点击switch开关打开按钮
    ……
}

isElementPresented 的封装:

public boolean isElementPresented(By by){
    boolean isDisplay = false;
    try{
        isDisplay = driver.findElement(by).isDisplayed();
    }catch (Exception e){
        isDisplay =false;
    }
    return isDisplay;
}

当这个按钮状态为关闭的时候运行出错

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. 

appium log:

[HTTP] --> POST /wd/hub/session/835154cb-e02a-473d-a3ee-5ea3437d8a98/element {"using":"xpath","value":"//android.view.ViewGroup/android.widget.ImageButton"}
[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: ["xpath","//android.view.ViewGroup/android.widget.ImageButton","835154cb-e02a-473d-a3ee-5ea3437d8a98"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.view.ViewGroup/android.widget.ImageButton","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.view.ViewGroup/android.widget.ImageButton","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding '//android.view.ViewGroup/android.widget.ImageButton' using 'XPATH' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":7,"value":"Could not find an element using supplied strategy. "}
[debug] [AndroidBootstrap] Received command result from bootstrap
[HTTP] <-- POST /wd/hub/session/835154cb-e02a-473d-a3ee-5ea3437d8a98/element 500 197 ms - 164 
[HTTP] --> POST /wd/hub/session/835154cb-e02a-473d-a3ee-5ea3437d8a98/element {"using":"xpath","value":"//android.view.ViewGroup/android.widget.ImageButton"}
[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: ["xpath","//android.view.ViewGroup/android.widget.ImageButton","835154cb-e02a-473d-a3ee-5ea3437d8a98"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.view.ViewGroup/android.widget.ImageButton","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.view.ViewGroup/android.widget.ImageButton","context":"","multiple":false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding '//android.view.ViewGroup/android.widget.ImageButton' using 'XPATH' with the contextId: '' multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":7,"value":"Could not find an element using supplied strategy. "}
[debug] [AndroidBootstrap] Received command result from bootstrap
[HTTP] <-- POST /wd/hub/session/835154cb-e02a-473d-a3ee-5ea3437d8a98/element 500 70 ms - 164 

有没有遇到过

@Lihuazhang @seveniruby


↙↙↙阅读原文可查看相关链接,并与作者交流