Appium 用 isdisplayed 判断元素是否存在的时候报错:An element could not be located on the page using the given search parameters.

William · 2017年02月10日 · 最后由 William 回复于 2017年02月11日 · 2431 次阅读

前置条件及环境如下

  • appium 版本:1.6.3
  • 运行平台:Win10
  • 测试平台:Android(6.0.1)

需求如下

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

  • 完全显示的时候 xpath 是: //android.view.ViewGroup/android.widget.ImageView
  • 不显示的时候 xpath 是://android.view.ViewGroup

我在判断的时候代码如下

//判断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

共收到 9 条回复 时间 点赞

我记得有个方法 isenable?试试?

—— 来自 TesterHome 官方 安卓客户端

#1 楼 @piaoransk 是有这么个方法,等我试试

还有一个 isVisible?都试一下呗

driver.findElement(by) 默认是找不到就报警. 不过你既然 try catch 了. 程序应该是没问题的. 不过是 appium 报警告而已的.
判断元素存不存在, 可以使用 driver.findElements(by) 如果没有返回的列表就是空的. 这样更简单点

#5 楼 @seveniruby try catch 了但是没有捕获异常,而是直接报错元素没找到。不知道为什么到这里是错误的,之前的用这个封装都没问题。很奇怪

是不是这样会好?
public boolean isElementPresented(By by) throws Exception

能把整个 stack trace 发出来看下吗?从你的 isElementPresented 方法来看,不应该在脚本端抛出这个异常(异常都被 catch 住了)。我觉得报异常的并不是你这个封装后的方法。

#8 楼 @chenhengjie123 解决了 前面没加显示等待

William 关闭了讨论 02月11日 11:30
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册