Errors with loop

I have the following problem. For tests I run two scripts, one long (about 400 commands) and one short (about 100 commands), each in a while loop. Without a loop the scripts work as expected and reliably. With a while-loop I need several hours to run the short script without an error. Sometimes layouts are not recognized or the Mobile.scrollToText command scrolls to the string and the script is aborted because the string was not found.The long script encounters an additional error. The wrong elements are changed, for example, layout 3 is changed instead of layout 5. The long script can be executed at least once in the best case.