在尝试使用Appium自动化我的应用程序时获取org.openqa.selenium.NoSuchElementException

如何解决在尝试使用Appium自动化我的应用程序时获取org.openqa.selenium.NoSuchElementException

在将我的问题标记为重复之前,请理解我已经遵循了关于stackoverflow的所有类似主题,包括:

org.openqa.selenium.NoSuchElementException: no such element

How to resolve org.openqa.selenium.NoSuchElementException in Selenium?

org.openqa.selenium.NoSuchElementException

org.openqa.selenium.NoSuchElementException Cannot find textbox element.

但是,尽管尝试了2天,我仍然无法解决该问题。

我将Eclipse与TestNG,Appium和uiautomatorviewer一起使用,以查看我的应用程序的布局详细信息。

这是我的基类代码:

public class BaseClass {

    static AppiumDriver<MobileElement> driver;
    @BeforeTest
    public void setup() throws Exception
    {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME,"ANDROID");
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION,"7.1.2");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Redmi");
        caps.setCapability(MobileCapabilityType.UDID,"6f13c427d440");
        caps.setCapability("appPackage","com.example.shipmart");
        caps.setCapability("appActivity","com.example.shipmart.MainActivity");
        caps.setCapability("autoGrantPermissions",true);
        caps.setCapability(MobileCapabilityType.APP,"C:\\Users\\Sparsh\\eclipse-workspace\\meriArt\\src\\test\\resources\\apps\\meri.apk" );
        caps.setCapability("automationName","uiautomator2");
    
    URL url = new URL("http://localhost:4723/wd/hub");
    
    driver = new AppiumDriver<MobileElement>(url,caps);
    
    }
    
    @AfterSuite
    public void teardown()
    {
        driver.close();
        driver.quit();
    }
}

这是我的TestNG测试类代码:

public class Tests extends BaseClass {

    @Test
    public void testOne()
    {
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

    MobileElement etSalary =driver.findElement(By.id("com.example.shipmart:id/etSalary"));
    etSalary.setValue("45555"); //THIS WORKS FINE

    MobileElement btnGo =driver.findElement(By.id("com.example.shipmart:id/btnGo"));
    //EXCEPTION OCCURS IN ABOVE LINE
    btnGo.click();
    System.out.println("ID of ImageButton is: " + btnGo.getId());
    
    }
}

第一个元素“ etSalary”已被检测到并填充有提供的值,但是在尝试找到第二个元素btnGo时发生异常。

这是完整的错误消息:

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
For documentation on this error,please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59',revision: 'e82be7d358',time: '2018-11-14T08:17:03'
System info: host: 'SPARSH',ip: '192.168.1.229',os.name: 'Windows 10',os.arch: 'amd64',os.version: '10.0',java.version: '1.8.0_261'
Driver info: io.appium.java_client.AppiumDriver
Capabilities {app: C:\Users\Sparsh\eclipse-wor...,appActivity: com.example.shipmart.MainAc...,appPackage: com.example.shipmart,autoGrantPermissions: true,automationName: uiautomator2,databaseEnabled: false,desired: {app: C:\Users\Sparsh\eclipse-wor...,deviceName: Redmi,platformName: android,platformVersion: 7.1.2,udid: 6f13c427d440},deviceApiLevel: 25,deviceManufacturer: Xiaomi,deviceModel: Redmi 4,deviceName: 6f13c427d440,deviceScreenDensity: 320,deviceScreenSize: 720x1280,deviceUDID: 6f13c427d440,javascriptEnabled: true,locationContextEnabled: false,networkConnectionEnabled: true,pixelRatio: 2,platform: LINUX,platformName: ANDROID,statBarHeight: 50,takesScreenshot: true,udid: 6f13c427d440,viewportRect: {height: 1230,left: 0,top: 50,width: 720},warnings: {},webStorageEnabled: false}
Session ID: f5c149f4-5da8-4923-953a-7cf404680701
*** Element info: {Using=id,value=com.example.shipmart:id/btnGo}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:61)
    at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:372)
    at io.appium.java_client.DefaultGenericMobileDriver.findElementById(DefaultGenericMobileDriver.java:69)
    at io.appium.java_client.AppiumDriver.findElementById(AppiumDriver.java:1)
    at org.openqa.selenium.By$ById.findElement(By.java:188)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:57)
    at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
    at tests.Tests.testOne(Tests.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(Unknown Source)
    at org.testng.TestRunner.privateRun(TestRunner.java:764)
    at org.testng.TestRunner.run(TestRunner.java:585)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.runSuites(TestNG.java:1069)
    at org.testng.TestNG.run(TestNG.java:1037)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

我尝试使用wait,使用xpath,类名,content-desc等来定位元素,将Java 10降级为Java 8,但我无法解决问题。

请帮助!

解决方法

可能会有多种情况显示此异常。

  1. 在上一个元素中发送键时,它是否在屏幕上?
  2. 如果将其发送给prev元素后将其启用或可见,则应使用显式等待来处理。或者,您可以尝试在找到buttongo元素之前添加睡眠。
  3. 按钮go元素可能位于屏幕上不可见,可能是因为您需要滚动Android页面并转到该元素。

希望这对您有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-