Android Appium:“无法使用给定的搜索参数在页面上定位元素”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25180309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:11:41  来源:igfitidea点击:

Appium: "An element could not be located on the page using the given search parameters" error

androidseleniumappium

提问by Aby

I am new to Appium and have been trying to automate the Conversion Calculator app for Android. Am getting the error "org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters", when trying to find a EditText element. Using Appium ver 1.0.0 and Android 4.3

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

The following is my code:

以下是我的代码:

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");

Even findElement by ID is not working. Please let me know what I am doing wrong here or if I need to provide more details.

甚至通过 ID findElement 也不起作用。请让我知道我在这里做错了什么,或者我是否需要提供更多详细信息。

回答by stsatlantis

I would use driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);instead of Thread.sleep(5000).

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

Try to use a newer version of Appium, I's been improved a lot. You can download the latest version of Appium and Appium clients here:http://appium.io/downloads.html

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

But be careful because in the newer version the findElement throws an Exception if there are more then one result of the search.

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



I would write this in a comment but I've not enough reputation :/

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

回答by Abhishek Swain

Possible Cause:

可能的原因:

  • Multiple EditText in the current screen.
  • 当前屏幕中的多个 EditText。

Please try with the following:

请尝试以下操作:

Solution1:

解决方案1:

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

0 - Index of EditText

0 - EditText 的索引

Solution2:

解决方案2:

Use any other locating strategy like Xpath.

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

回答by Rusty Wizard

Maybe you could try waiting until the element is visible or enabled using a WebDriverWait object?

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