【问题标题】:Element not found with webdriver使用 webdriver 找不到元素
【发布时间】:2018-09-19 20:34:56
【问题描述】:

我正在使用 Java 使用 Appium 自动化一个 android 应用程序。我的情况是,我需要单击按钮 1 或按钮 2,以存在者为准

Appium 错误日志:[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] 未能定位元素。清除辅助功能缓存并重试。 [debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] 发现 '//android.widget.ImageButton[@resource-id='net.ilius.android.meetic:id/profileMailPremiumButton']' 使用 'XPATH' 和 contextId: '' multiple: false

if (driver.findElementByXPath("//android.widget.ImageButton[@resource-id='net.ilius.android.meetic:id/profileMailPremiumButton']")
        .isDisplayed()) {
     driver.findElementByXPath("//android.widget.ImageButton[@resource-id='net.ilius.android.meetic:id/profileMailPremiumButton']")
        .click();
} else {
    driver.findElementById("net.ilius.android.meetic:id/profileMailButton").click();
}

【问题讨论】:

  • 请创建一个minimal reproducible example。特别是包括完整的错误信息和 HTML 的相关部分。
  • @sandy : 使用 findElements 和 isEmpty 方法检查元素是否存在
  • Locator Strategy findElementByXPath("//android.widget.ImageButton[@resource-id='net.ilius.android.meetic:id/profileMailPremiumButton']")findElementById("net.ilius.android.meetic:id/profileMailPremiumButton") 不是指同一个元素吗?
  • @DebanjanB 已编辑。对此感到抱歉

标签: java selenium-webdriver appium


【解决方案1】:

如果您使用 isDisplayed() 并且 UI 上不存在元素它将引发异常 - 找不到元素

因此,首先使用 findElements 检查该元素是否存在:driver.findElements(selector).isEmpty()

如果它为空,则表示元素现在不可用,您可以转到 else 块

使用这段代码: `

if (!driver.findElements(By.xPath("//android.widget.ImageButton[@resource-id='button1']")).isEmpty()) {
     driver.findElementByXPath("//android.widget.ImageButton[@resource-id='button1']").click();
} else {
    driver.findElementById("button2").click();
}`

【讨论】:

  • 我收到错误:[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] 无法定位元素。清除辅助功能缓存并重试。
  • 是的。事实上,我需要点击任何存在的按钮。逻辑是只有一个按钮会出现。
  • 它仍在搜索“button1”。
  • 试试这个-------- if (!driver.findElements(By.xPath("//android.widget.ImageButton[@resource-id='button1']")) .isEmpty()) { if(driver.findElementByXPath("//android.widget.ImageButton[@resource-id='button1']").isDisplayed()) driver.findElementByXPath("//android.widget.ImageButton[ @resource-id='button1']").click(); } else { driver.findElementById("button2").click(); }`
  • 它的代码和你上面提到的一样吧?我试过了,在执行时,当按钮 2 仅存在于应用程序中时,它会搜索按钮 1。
猜你喜欢
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 2016-09-07
相关资源
最近更新 更多