当前位置: 首页 > 知识库问答 >
问题:

获取组织。openqa。硒。尝试使用Appium自动化我的应用程序没有什么例外

禄仲渊
2023-03-14

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

组织。openqa。硒。NoSuchElementException:没有这样的元素

如何解析组织。openqa。硒。硒中的非接触元素异常?

org.openqa.selenium.NoSuchElementException

org.openqa.selenium.NoSuchElementException找不到文本框元素。

然而,我无法解决这个问题,尽管我试图解决它,因为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..., appActivity: com.example.shipmart.MainAc..., appPackage: com.example.shipmart, autoGrantPermissions: true, automationName: uiautomator2, 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, platformVersion: 7.1.2, 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、classname、content desc等来定位元素,将Java 10降级为Java 8,但我无法解决这个问题。

请帮忙!

共有1个答案

舒阳州
2023-03-14

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

  1. 当您在上一个元素中发送键时,屏幕上是否显示此元素
  2. 如果在将键发送到prev元素后它变得启用或可见,那么您应该使用显式等待来处理它。或者你可以试着在找到buttongo元素之前添加sleep
  3. Button go元素可能位于屏幕中,但不可见。可能您需要滚动Android页面并转到该元素

希望这对你有帮助。

 类似资料: