【问题标题】:Appium: "An element could not be located on the page using the given search parameters" errorAppium:“使用给定的搜索参数无法在页面上找到元素”错误
【发布时间】:2014-09-30 13:33:04
【问题描述】:

我是 Appium 的新手,一直在尝试自动化 Android 的 Conversion Calculator 应用程序。尝试查找 EditText 元素时,出现错误“org.openqa.selenium.NoSuchElementException:无法使用给定的搜索参数在页面上找到元素”。使用 Appium 1.0.0 版和 Android 4.3

以下是我的代码:

List<WebElement> textViews = driver.findElements(By.className("android.widget.TextView"));
for (i=0; i<textViews.size(); i++) {
  if(textViews.get(i).getText().toLowerCase().contains("memory")) {
    textViews.get(i).click();
  }
} 
Thread.sleep(5000);

WebElement editText = driver.findElement(By.className("android.widget.EditText"));
editText.sendKeys("123");

即使按 ID 查找元素也不起作用。请让我知道我在这里做错了什么,或者我需要提供更多详细信息。

【问题讨论】:

  • NoSuchElementException 通常有几个原因。 1. 元素在frame 或iframe 内。 2. 元素加载缓慢。

标签: android selenium appium


【解决方案1】:

我会使用 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 而不是 Thread.sleep(5000)。

尝试使用更新版本的Appium,我已经进步了很多。您可以在这里下载最新版本的 Appium 和 Appium 客户端:http://appium.io/downloads.html

但要小心,因为在较新的版本中,如果搜索结果不止一个,findElement 会抛出异常。


我会在评论中写这个,但我没有足够的声誉:/

【讨论】:

  • 我在使用 appium 和量角器时遇到了同样的问题,我还要添加 sleep(10000);例如 ?我在 mac 中使用最新版本的 appium 和 ipad 模拟器
  • 不推荐使用thread.sleep,driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);的方法你有运气吗?
【解决方案2】:

可能原因:

  • 当前屏幕中有多个 EditText。

请尝试以下方法:

解决方案 1:

List<WebElement> editText = driver.findElements(By.className("android.widget.EditText"));
editText.get(0).sendKeys("123");

0 - EditText 的索引

解决方案 2:

使用任何其他定位策略,例如 Xpath。

【讨论】:

    【解决方案3】:

    也许您可以尝试等到元素可见或使用 WebDriverWait 对象启用?

    【讨论】:

      【解决方案4】:

      尽量避免使用睡眠,尝试使用 WAIT 命令。 即使元素已经在屏幕上,也无需等待已确定的时间即可休眠。 在等待命令的情况下,一旦元素出现,动作就已经执行,这沿着代码将大大减少执行时间。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-23
        • 2016-12-26
        • 2017-01-15
        • 2019-08-30
        • 2021-09-29
        • 1970-01-01
        • 2019-08-18
        相关资源
        最近更新 更多