Search code examples
androidseleniumappiumappium-android

Getting org.openqa.selenium.NoSuchElementException on trying to automate my app using Appium


Before you mark my question as duplicate, please understand that I have followed all the similar topics on stackoverflow including:

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.

Yet, I'm not able to solve the issue inspite of trying to solve it since 2 days.

I'm using Eclipse with TestNG, Appium and uiautomatorviewer to view the layout details of my app.

This is my code for base class:

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

This is my code for the class with TestNG tests:

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

The first element 'etSalary' is getting detected and filled with provided value, but exception occurs while trying to locate the second element btnGo.

This is the complete error message:

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)

I have tried using wait, using xpath, classname, content-desc etc. to locate the element, downgraded Java 10 to Java 8, yet I'm not able to solve the issue.

PLEASE HELP!


Solution

  • There might be multiple scenario why it's showing this exception.

    1. Is this element on the screen when you sending keys in the previous element.
    2. If it get enable Or visible after sending keys to prev element then you should use explicit wait to handle that. Or you can just try to add sleep before find the buttongo element.
    3. Button go element might be located in the screen that is no visible might be you need to scroll the Android page and go to that element.

    Hope this might help you.