gpt4 book ai didi

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters(使用给定的搜索参数在页面上找不到元素)

转载 作者:bug小助手 更新时间:2023-10-24 22:14:26 27 4
gpt4 key购买 nike



I am currently working something on automation testing of some small mobile application and first time trying to implement Appium.

我目前正在对一些小型移动应用程序进行自动化测试,这是我第一次尝试实现Appium。


I have a trouble when running the test with the following error - org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.

在运行测试时遇到了问题,出现以下错误-org.Openqa.selenium.NoSuchElementException:使用给定的搜索参数无法在页面上找到元素。


Tried many things, but no success. I have installed the Appium server locally version 2.1.3 which is the latest at the moment. I also have Appium Inspector installed where I get the resource Ids for the elements to access later in the tests.

尝试了很多方法,但都没有成功。我已经在本地安装了Appium服务器版本2.1.3,这是目前最新的版本。我还安装了Appium Inspector,在那里我可以获得稍后在测试中访问的元素的资源ID。


Here is the test example I am trying

以下是我正在尝试的测试示例


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()
}
}

I have tried also implicit waiting, thread sleeping, trying different selectors but nothing helped me.
Here in the inspector we can see the resource id of the element I am trying to access.

我也尝试了隐式等待、线程休眠、尝试不同的选择器,但都没有帮助。在检查器中,我们可以看到我试图访问的元素的资源ID。


Picture of Appium Inspector

Appium检查员的图片


NOTE: I am using a virtual device created as part of the Device Manager in Android Studio, and in debugging mode the AndroidDriver is initializing okay, when it comes to the part to get the element (noted as fragment in the example above) it crushes.

注意:我使用的是Android Studio中作为设备管理器的一部分创建的虚拟设备,在调试模式下,Android驱动程序初始化正常,当涉及到获取元素(在上面的示例中记为片段)的部分时,它会崩溃。


Thank you in advance :))

提前感谢您:))


更多回答
优秀答案推荐

It would help if you did not put all path of the element,

如果您不将元素的所有路径


Could you try this?

你能试一下这个吗?


val fragment = driver.findElement(By.id("favoriteRecipesFragment"))

更多回答

Didn't had any success with this one. It still throws the error NoSuchElementException...

在这件事上没有任何成功。它仍然抛出错误NoSuchElementException...

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com