Appium移动端自动化测试遇NoSuchElementException问题求助
Appium自动化测试元素定位失败求助
首次使用Appium开展小型移动应用自动化测试,运行测试时抛出如下错误:
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
环境说明
- 本地安装最新版Appium Server 2.1.3
- 通过Appium Inspector获取元素resource Id
- 测试设备为Android Studio设备管理器创建的虚拟设备,AndroidDriver初始化正常,但定位标记为fragment的元素时程序崩溃
已尝试方案
隐式等待、线程休眠、更换不同选择器,均未解决问题。Appium Inspector截图中可确认目标元素的resource Id存在。
测试示例代码
package com.example.myfood_kotlin.appiumtests.foodjokes.favorites import io.appium.java_client.android.AndroidDriver import io.appium.java_client.remote.AndroidMobileCapabilityType import io.appium.java_client.remote.MobileCapabilityType import org.junit.Test import org.openqa.selenium.By import org.openqa.selenium.remote.DesiredCapabilities import org.openqa.selenium.support.ui.ExpectedConditions import org.openqa.selenium.support.ui.WebDriverWait import java.net.URL import java.time.Duration class FavoritesRecipesAppiumTest{ @Test fun testRecyclerViewIsDisplayed() { // my-ip-address should be replaced with the appropriate ip - see from appium command terminal val appiumServerUrl = URL("http://(my-ip-address):4723") // NOTE: when using appium 2.* version the default remote path is / (not /wd/hub) val capabilities = DesiredCapabilities() capabilities.setCapability("device", "selendroid"); capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android") capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.example.myfood_kotlin") capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.example.myfood_kotlin.ui.MainActivity") capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2") capabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS, true) capabilities.setCapability(MobileCapabilityType.NO_RESET, true) val driver = AndroidDriver(appiumServerUrl, capabilities) // Thread.sleep(4000) // val wait = WebDriverWait(driver, Duration.ofSeconds(10)) // wait.until(ExpectedConditions.presenceOfElementLocated(By.id("com.example.myfood_kotlin:id/search_button"))) // Click on the favorites button (assuming it's an element with resource id) val fragment = driver.findElement(By.id("com.example.myfood_kotlin:id/favoriteRecipesFragment")) fragment.click() // Verify that the RecyclerView is displayed (assuming it's an element with resource id) // val view = driver.findElement(By.id("com.example.myfood_kotlin:id/favoriteRecipesRecyclerView")) // view.isDisplayed // Thread.sleep(4000) driver.quit() } }
求助元素定位失败的原因及解决方法,感谢帮助!
内容的提问来源于stack exchange,提问作者Lazar Todorov
相关产品推荐
相关产品推荐

