[{"data":1,"prerenderedAt":20},["ShallowReactive",2],{"blog-post-en-handle-pagination-infinite-scroll-phoma-flows":3},{"slug":4,"lang":5,"title":6,"summary":7,"date":8,"tags":9,"tag_slugs":15,"thumbnail_url":16,"translations":17,"body":18,"asset_base":19},"handle-pagination-infinite-scroll-phoma-flows","en","Handle Pagination and Infinite Scroll in Phoma Flows with Loops and Conditions","Learn to build resilient Phoma flows that automatically scroll through paginated lists and infinite‑scroll screens using loops, conditional waits, and stable element selectors.","2026-11-10",[10,11,12,13,14],"phoma","android-automation","qa-testing","flow-building","ui-selectors",[10,11,12,13,14],"https://blog-api.phomaapp.com/api/blog/posts/handle-pagination-infinite-scroll-phoma-flows/thumbnail.svg?lang=en",[5],"## Why Pagination and Infinite Scroll Break Simple Flows\n\nMost Phoma tutorials show a single tap or a short swipe. Real‑world apps, however, present data in long lists that load incrementally — either by a “Load more” button (pagination) or by automatically fetching new items when the user reaches the bottom (infinite scroll). A flow that only swipes once will miss the majority of the data, and a flow that repeats a fixed number of swipes will either stop too early or keep swiping after the list ends, causing errors or infinite loops.\n\nThe root cause is that the UI tree changes after each load: new `ViewHolder` objects appear, old ones may be recycled, and the scroll container’s bounds shift. To automate reliably you need a flow that **observes the UI after each interaction**, decides whether more content exists, and stops only when a genuine end‑of‑list signal appears.\n\n## Core Concepts: Loops, Conditions, and Wait Steps in Phoma\n\nPhoma’s visual builder provides three primitives that map directly to this problem:\n\n| Primitive | Purpose | Typical Setting for Lists |\n|-----------|---------|---------------------------|\n| **Loop** | Repeat a block while a condition holds | `While element \"listContainer\" exists` |\n| **Condition** | Branch based on a UI state | `If element \"noMoreItems\" is visible → Break` |\n| **Wait for Element** | Pause until a selector matches (with timeout) | `Wait for new item selector, timeout 5 s` |\n\nAll three operate on **element selectors** you define once (see the selector section below). Because the flow runs on your Windows machine, the loop runs locally — no cloud latency — and you can watch the mirrored screen to verify each iteration.\n\n## Step‑by‑Step: Building a Flow That Scrolls a RecyclerView Until the Last Item\n\n### Prerequisites\n* Windows PC with Phoma installed (free tier is enough).\n* Android device connected via USB (USB debugging on) or Wi‑Fi ADB.\n* Target app installed and the screen you want to scrape reachable from the home screen.\n\n### 1. Connect the Device and Open the App\n1. In Phoma, click **Add Device** → select your phone → **Connect**.\n2. Add a **Launch App** step: package `com.example.app`, activity `MainActivity`.\n3. Add a **Wait for Element** step for the list container so the flow doesn’t start scrolling before the UI is ready.\n\n```json\n{\n  \"type\": \"waitForElement\",\n  \"selector\": {\n    \"strategy\": \"uiautomator\",\n    \"value\": \"new UiSelector().resourceId(\\\"com.example.app:id/recyclerView\\\")\"\n  },\n  \"timeoutMs\": 8000\n}\n```\n\n### 2. Identify the List Container and a Representative Item Selector\nOpen the **Element Inspector** (mirrored screen → click the inspector icon). Click the `RecyclerView` → copy its selector (resource‑id is ideal). Then click a single list item → copy its selector. You’ll use the container for the loop condition and the item for data extraction.\n\n*Container selector* (stable across updates):\n```text\nnew UiSelector().resourceId(\"com.example.app:id/recyclerView\")\n```\n\n*Item selector* (relative to container, used each iteration):\n```text\nnew UiSelector().resourceId(\"com.example.app:id/itemTitle\")\n```\n\n### 3. Add a Loop Step That Continues While the Container Exists\nDrag a **Loop** block onto the canvas. Set its condition to **Element Exists** → paste the container selector. Phoma will re‑evaluate this selector at the start of every iteration.\n\n### 4. Inside the Loop: Swipe, Wait, Extract\n1. **Swipe Up** – choose the container element as the swipe target, direction **Up**, distance **80 %** of screen height, duration **300 ms**.\n2. **Wait for New Items** – add a **Wait for Element** step using the *item selector* with a short timeout (3–5 s). This ensures the flow pauses until the next batch renders.\n3. **Extract Data** – add a **Read Text** step (or **Get Attribute**) on the *item selector*. Enable **“All matches”** so Phoma returns an array of titles for the current viewport.\n4. **Store Results** – use a **Set Variable** step (name `collectedTitles`) with expression `{{collectedTitles}} + {{lastReadText}}` to accumulate across iterations.\n\n### 5. Add a Break Condition for the End of List\nMost apps show a footer like “No more items” or hide the scrollbar when the list ends. Inspect that element and add a **Condition** step **inside the loop** (after the wait):\n\n```json\n{\n  \"type\": \"condition\",\n  \"if\": {\n    \"strategy\": \"uiautomator\",\n    \"value\": \"new UiSelector().text(\\\"No more items\\\")\"\n  },\n  \"then\": [ { \"type\": \"breakLoop\" } ],\n  \"else\": []\n}\n```\n\nIf your app doesn’t show an explicit message, you can break when the **scroll position** stops changing. Phoma exposes a **Get Scroll Position** action on the container; compare the value from the previous iteration (store in a variable) and break when they’re equal for two consecutive loops.\n\n### 6. Finish the Flow\nAfter the loop ends, add a **Log** or **Export Variable** step to write `collectedTitles` to a CSV file on your PC (Phoma’s **Write File** action). Optionally, close the app.\n\n## Handling Infinite Scroll with a “Load More” Button\nSome screens use a button instead of auto‑load. The pattern is identical, only the interaction changes:\n\n1. After the **Wait for Element** step, add a **Condition** that checks for the button selector (`resourceId=\"loadMoreBtn\"`).\n2. If present → **Click** the button → **Wait for Element** (new items) → continue loop.\n3. If absent → **Break** (end of data).\n\nThis keeps the flow deterministic even when the button appears only after a certain scroll offset.\n\n## Dealing with Dynamic Content and Stale Elements\nBecause Android recycles `ViewHolder`s, an element reference captured in iteration 1 may become stale in iteration 2. Phoma solves this by **re‑querying selectors on every step**. Ensure you never store a raw element handle in a variable; always store *selector strings* and let Phoma resolve them at runtime. If you need to act on a specific item (e.g., click the 5th title), compute the index inside the loop and use a **Child Selector**:\n\n```text\nnew UiSelector().resourceId(\"com.example.app:id/recyclerView\")\n    .childSelector(new UiSelector().resourceId(\"com.example.app:id/itemTitle\").instance(4))\n```\n\n## Best Practices for Stable Selectors Across App Updates\n* Prefer **resource‑id** over `className` or `index`.\n* When an id is missing, use **content‑desc** (accessibility label) – it’s rarely changed.\n* Avoid absolute XPath‑like paths (`/android.widget.FrameLayout/...`).\n* Test selectors in the Element Inspector after each app release; Phoma highlights matches in real time.\n\n## Troubleshooting Common Issues\n| Symptom | Likely Cause | Fix |\n|---------|--------------|-----|\n| Loop never exits | Break condition selector never matches | Verify the “no more” text exactly (case‑sensitive) or add a scroll‑position comparison fallback. |\n| Swipe does not move list | Swipe target is a child view that doesn’t scroll | Set swipe target to the `RecyclerView` container, not an item. |\n| “Element not found” after a few iterations | Selector uses `instance(n)` that shifts as items recycle | Switch to a relative selector (e.g., `childSelector` without fixed instance) or use `textContains` for the title. |\n| Flow runs very slowly | Timeout on Wait for Element too high | Reduce timeout to 3 s; the list usually renders within 1 s on modern devices. |\n\n## Extending the Pattern\n* **Horizontal carousels** – change swipe direction to **Left** and use a horizontal `RecyclerView` selector.\n* **Grid layouts** – swipe up still works; just ensure the container selector matches the `GridLayoutManager` view.\n* **Nested lists** (e.g., expandable sections) – add an outer loop over section headers, inner loop over each section’s items.\n\n## Summary Checklist\n- [ ] Capture stable container and item selectors via Element Inspector.\n- [ ] Build a **Loop → Swipe → Wait → Extract → Condition → Break** block.\n- [ ] Accumulate data in a variable; export at the end.\n- [ ] Test on at least two device models (different screen densities).\n- [ ] Add the flow to your team’s shared library (Team/Business plan) for reuse.\n\nWith this structure you can automate any paginated or infinite‑scroll screen — product catalogs, chat histories, log viewers — without writing a single line of code. The flow lives entirely on your Windows machine, runs on your own phones, and can be scheduled or triggered manually whenever you need fresh data.\n","https://blog-api.phomaapp.com/api/blog/posts/handle-pagination-infinite-scroll-phoma-flows/assets",1796035641683]