NoSuchELementException when element is actually present

HI, I am facing this strange issue. While trying to capture one particular object on a page in my Android native app through appium, the page is stuck on loading on the appium screen, though in the actual device, it navigates to the next page. Then I refresh my appium screenshot to get the expected page and capture the locators. However, while I am running the script using the locator captured this way, it throws the nosuch element exception though the page loads well in the android device. Any help would be appreciated.

org.openqa.selenium.NoSuchElementException: Can’t locate an element by this strategy: Locator map:

  • native content: “By.xpath: //android.widget.TextView[@text=‘TEXT’]”
  • html content: "by id or name “TEXT”

@PoornimaSuraj add your code…

@AndroidFindBy(xpath="//android.widget.TextView[@text='TEXT']")
@iOSFindBy()
public WebElement MyObject;

super.waitForVisibility(MyObject);

To give a bit more information, this is in a window that opens up on clicking a button from a popup. I tried multiple objects from that page, but everything is giving the same error

@PoornimaSuraj your code does not look good. can you post the whole into https://gist.github.com/ and share link. you can remove all sensitive info if any.

in general you trying to use page object. but what you wrote i never saw :slight_smile:

Sorry for that Aleksei, my code is distributed among some files,which is why I couldnt give a lot of clarity. Meanwhile I just tried with the following hardcoded code, which also did not work with elements on this page.
driver.findElement(By.xpath("//android.widget.TextView[@text=‘TEXT’]")).click();

@PoornimaSuraj try make it easier:

        List<WebElement> el = driver.findElements(MobileBy.AndroidUIAutomator("new UiSelector().text(\"MY_TEXT\")"));
        if (el.isEmpty()) {
            System.out.println("el with 'MY_TEXT' not found");
            return;
        }
        System.out.println("tap 'MY_TEXT'");
        new TouchAction(driver).press(el.get(0)).waitAction(Duration.ofMillis(70)).release().perform();

if you fail to find your element with this check element attributes with some android pageSourceviewer e.g. uiautomatorviewer.

I tried to check the element attributes using uiautomatorviewer and appium, and they are both giving me the same values that I am using for my script. Will try like you mentioned

Tried that piece of code too , it was unable to locate the element inspite of it being displayed.
el with ‘MY_TEXT’ not found

super(DriverManager.getAndroidDriver());
PageFactory.initElements(new AppiumFieldDecorator(DriverManager.getAndroidDriver(), 5, TimeUnit.SECONDS), this);

@PoornimaSuraj did you replace “my_text” with correct text in code?

What is the Android version? If its 7 or more then include the below in your Desired Capabilities file.

automationName: 'uiautomator2'

Hi Vekatesh_Akula,I am using Android version 7.0. I tried with the capability uiautomator2, however I am getting error forwarding the new session empty pool of VM for setup capabilities. Please see below the set of capabilities I used:

“app”: “myapp.apk”
“automationName”: “uiautomator2”
“browserName”: “SamsungGalaxyS7”
“udid”: “ce11160bbbdf5c2d02”
“platformName”: “Android”
“deviceName”: “SamsungGalaxyS7”

What could I be doing wrong here?

Thanks a bunch @Venkatesh_Akula : After a couple of restarts and grid/node refreshes, I was able to get the code working with uiautomator2 and yes, the objects are getting identified now. :slight_smile:

1 Like

This issue can be closed. Thanks @Aleksei and @Venkatesh_Akula

I’m facing exactly same error with iOS simulator, what can be the root cause ?

Hello Poornima,

I’m running on Appium 1.8.1 and Java Appium Client 6.1.0. I started using UIAutomator2.
I’m experiencing the same behaviour i.e. element not found while I see the element is available page source shows the element’s resource id.

I read your comment here, saying restarts and grid/node refresh fixed it for you.
Kindly can write bit more in detail what did you and how you did that. It will help me!

I tried restarting npm on my machine but I continue to see the same behaviour.

Hello @ravisuriya : I did not get ‘element not found’ after using uiautomator2. I only had “error forwarding the new session empty pool of VM for setup capabilities” during the start of the script which got fixed after restarts. Could have been version compatibility or something. Could you elaborate more on the exact issue you are facing so that I can help if it gives me any clue.

@Venkatesh_Akula

public class Calling {
AndroidDriver dr;
@Test
public void call() throws MalformedURLException, WebDriverException, SocketException, InterruptedException
{

	DesiredCapabilities cp= new DesiredCapabilities();
	cp.setCapability("deviceName", "Moto C Plus");
	cp.setCapability("platformVersion", "7.0");
	cp.setCapability("platformName", "Android");
	cp.setCapability("appPackage", "com.motorola.launcher3");
	cp.setCapability("appActivity", "com.android.launcher3.GoogleNowPanel");
	
	dr = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"),cp);
	
	dr.findElement(By.xpath("//android.widget.TextView[@text='Phone']")).click();
	dr.findElement(By.id("com.android.dialer:id/floating_action_button")).click();
	dr.findElement(By.id("com.android.dialer:id/eight")).click();
	dr.findElement(By.id("com.android.dialer:id/eight")).click();
	dr.findElement(By.id("com.android.dialer:id/nine")).click();
	dr.findElement(By.id("com.android.dialer:id/eight")).click();
	dr.findElement(By.id("com.android.dialer:id/two")).click();
	dr.findElement(By.id("com.android.dialer:id/seven")).click();
	dr.findElement(By.id("com.android.dialer:id/seven")).click();
	dr.findElement(By.id("com.android.dialer:id/four")).click();
	dr.findElement(By.id("com.android.dialer:id/five")).click();
	dr.findElement(By.id("com.android.dialer:id/four")).click();
	
	dr.findElement(By.id("com.android.dialer:id/dialpad_floating_action_button")).click();
	Thread.sleep(12000);
	dr.findElement(By.id("com.android.dialer:id/floating_end_call_action_button")).click();
	
}

}

I am getting below error everytime i run the code but it seems that there is no error in the typed code:

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: ‘3.8.1’, revision: ‘6e95a6684b’, time: ‘2017-12-01T19:05:14.666Z’
System info: host: ‘DESKTOP-A54NRMM’, ip: ‘192.168.1.106’, os.name: ‘Windows 10’, os.arch: ‘amd64’, os.version: ‘10.0’, java.version: ‘1.8.0_151’
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities {appActivity: com.android.launcher3.Googl…, appPackage: com.motorola.launcher3, databaseEnabled: false, desired: {appActivity: com.android.launcher3.Googl…, appPackage: com.motorola.launcher3, deviceName: Moto C Plus, platformName: Android, platformVersion: 7.0}, deviceManufacturer: motorola, deviceModel: Moto C Plus, deviceName: ZW2224KZ5W, deviceScreenSize: 720x1280, deviceUDID: ZW2224KZ5W, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, platform: LINUX, platformName: LINUX, platformVersion: 7.0, takesScreenshot: true, warnings: {}, webStorageEnabled: false}
Session ID: af9723d7-30cb-47dc-b309-018247786630
*** Element info: {Using=xpath, value=//android.widget.TextView[@text=‘Phone’]}

Please let me how to resolve this issue.
Following is my appium version: 1.6.2
Following is my Eclipse version: Version: 3.7.3.201704251225

Which line is the script failing? Will be good if you can attach the screenshot of the app and full Appium logs. This looks like an incorrect locator

@Venkatesh_Akula
I have attached the appium logs and the picture of the failing line.

appium-server-logs.txt (16.4 KB)