Strange behavior finding element

I am using Appium (on nodejs - using “wd” npm package) to automate an android app.

I am trying to find TextView element (using appium on nodejs). It doesnt find element when i run the code via nodejs. However, i am able to find the element using appium inspector.
:worried: So it works using inspector but don’t works in my nodejs script. Tried finding with ID and also using UISelector().text() … Both ways dont work in the nodejs script while they work perfectly in appium inspector.

This behavior is very strange. Even appium inspector uses the same “wd” methods to locate the element and it succeeds.

Any hint of what could be wrong?

Probably a timing issue. Often Appium will try to find an element before the screen is fully rendered. You should look into ‘implicit’ waits:

http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

Implicit wait is already set to 2 seconds. The strange is that, there is a ListView which is always present and visible in the app but even that cannot be found in the script by the appium.

Since an implicit wait will always return immediately once the element is rendered, why not set it to 30? You may find it returns in 2, 5, or 10 seconds depending on device/circumstances.

I tried that. That also didn’t work. Note that I am testing it on a android virtual device. Setting implicit wait time to even 30s didnt work. I am still confused about the fact that appium inspector works fine for locating the element.

Strange case for sure. I’m going to recommend you wrap your locator in a try/catch block and in the catch you grab the the entire screen for inspection (driver.getPageSource()). That way you can really see what you are up against.

Well. I tried getting the source. It didn’t return and kept on waiting forever. The call to getPageSource() took forever that I had to stop the script manually after waiting for a lot of minutes.

Try to do that in debug mode from your IDE. If you have no results maybe post a gist file with complete logs in here or open directly a possible issue in https://github.com/appium/appium/issues

You should post your code as well, and a lot more details. What version of Appium? uiautomator1/2? Was this working before? Android version?

The more details the better.

Appium version: Appium v1.7.1

Capabilities:

    {
        browserName: '',
        'appium-version': '1.7.1',
        platformName: 'Android',
        platformVersion: '7.0',
        deviceName: 'Android Emulator',
        appPackage: "com.example.android",
        appActivity: ".MainActivity",
        avd: "Nexus_5X_API_24",
        avdLaunchTimeout: 300000,
        avdReadyTimeout: 300000
    }

The following are the logs which result when i try to get the page source.

Log:

[debug] [MJSONWP] Calling AppiumDriver.getPageSource() with args: ["25bc4dbc-5f8b-458d-a28a-d9c09e3e48ba"]

[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"source","params":{}}

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"source","params":{}}

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: source

It gets stuck here. and no more logs are produced which means its waiting like forever here.

Code:

let source = await driver.source();
console.log("source: ",  source);

My script works like this:

    // the following code works perfectly fine (for logging in to the app)
    let loginBtn = await driver.elementById("com.example.android:id/login_btn");
    await loginBtn.click();
    let usernameInput = await driver.elementById("com.example.android:id/login_user");
    await usernameInput.click();
    await usernameInput.sendKeys(program.username);
    let passwordInput = await driver.elementById("com.example.android:id/password");
    await passwordInput.click();
    await passwordInput.sendKeys(program.password);
    let loginSubmitBtn = await driver.elementById("com.example.android:id/submit_btn");
    await loginSubmitBtn.click();
    // ... now after login, a ListView element is loaded which has my target element. At this point, the call to get page source is never completed and takes forever. 
    // code execution is stuck at the following line
    let source = await driver.source(); // takes forever. also tried locating the target element here which is not found


I can confirm it works fine using Appium Inspector :frowning: and this is my first time trying appium.

Ok, first let me say that I am by no means a javascript expert. These days I do almost everything in Ruby, and in the past I have worked with Java.

To me it looks like you are making the right calls. Do you know what version of the Javascript client you are using? I’m seeing the latest ‘tag’ as v1.5.0:

And here is the ‘source’ command you are using:

Could it be that this app goes into a WebView or something? I agree with @Telmo_Cardoso that you might be looking at a bug.

The app that I am using this on is Instagram. I was using “wd” version 1.4.0. I have tried upgrading and that made no difference.

I tried a number of things. I found out that If I interact with the app manually, the frozen call is completed and then the code works fine. For example, I navigated to the “Search” tab of Instagram and focused the search term input (also accepted the Location permission). At this point the code flow was resumed and frozen call to .source() was completed and the getElement call also started working after this. It works fine after this. This is consistent. When I do this for each session, it works as expected.

Any idea of what is wrong here?

hi,
I am too facing the exactly same issue, now have you solved your problem? if yes can you please share the solution for that? I am having the issue with the ideal time of the appium server, it keeps on finding the element but never return any thing and when done manual interfere it proceeds with the further code, please share if somebody have some solution!
Thanks in advance