Appium can't locate dynamic generated elements on iOS native app

There is an UILabel element X in our app that is not visible when the app start. After you clicked a button in the app then the label X become visible.

When I start a new session of the app with Appium, I can locate all other 5 elements under the same XCUIElementTypeCell. Then I click the button to make the label X visible and Refresh Appium to retrieve the updated page source. The label X appeared on the app, but not detected by Appium. There were still just 5 elements under that XCUIElementTypeCell. No matter how many time I refresh the App Source, it still remain relocatable.

Is there a way I can enforce Appium to reset and recreate the page source of the app??

The is a way. First we need your code how you detect elements before and after click.

I am using page object model with yiewd

module.getElementByAccessibilityID = o_O(function* (ID, max_attempts) {
        let cond = yield driver.hasElementByAccessibilityId(ID);
        while (cond == false && max_attempts > 0) {
            yield driver.setImplicitWaitTimeout(100);
            cond = yield driver.hasElementByAccessibilityId(ID);
            max_attempts--;
        }
        let el = yield driver.elementByAccessibilityId(ID);
        return el;
    });

I tried with the following function as well. Get page source if the element is not found. I checked the output of the source file, it did not contain the element I am after.

module.getElementByAccessibilityID = o_O(function* (ID, max_attempts) {
        let cond = yield driver.hasElementByAccessibilityId(ID);
        while (cond == false && max_attempts > 0) {
            yield driver.setImplicitWaitTimeout(500);
            var source = yield driver.source();
            //console.log(source);
            fs.writeFile('C:/logs/source.txt', source, function (err) {
                if (err) return console.log(err);
                console.log('Source > source.txt');
            });
            cond = yield driver.hasElementByAccessibilityId(ID);
            max_attempts--;
        }
        let el = yield driver.elementByAccessibilityId(ID);
        return el;
    });

iOS is always tricky and has problems. some elements still can’t be visible even with native automation (e.g. when we put some elements inside button element it can be problematic to see them. unfortunately we also have this problem with our client and thus i was need re-wrote few tests in some different way to check it values).

you can check this be following trick:

  1. start your test but do nothing. just start driver.
    do loop:
  2. put test in some pause in code e.g. for 20sec
  3. after pause add print pagesource in console
  4. manually go with client to needed screen and wait pagesource to see

repeat 4 step with some other actions and check pageSource. if nothing = you are not lucky.

I tried the wait then get pagesource and no luck. But when I try to act click to a button then get pagesource, it worked.

I looked into the Appium log and figured that if the session Id got update after the action then it would refresh the pagesource. The problem is not all actions would trigger session Id to be updated. I guess I’ll have to keep trying and figure out which action would trigger update of the session whenever I try to locate a new dynamic generated element.

Hello
Any progress? I have similar problem.

Did something succeed with that?

Still having the same problem… :frowning: Is there any progress on this?

Hi,
What’;s wrong with this code. can someone help me pls?

String sWiFiScreenHeadingText1 = “Select Your WiFi”;
elementToGetText = driver.findElement(MobileBy.xpath("//android.widget.TextView[contains(@text,’ " + sWiFiScreenHeadingText1 + " ')]"));