在尝试使用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页面并转到该元素。

希望这对您有所帮助。