Repeatedly click a button until no new content appears or max clicks reached.This function is useful for “Load More” buttons or paginated content where you need to
keep clicking until all content is loaded. It provides several stopping conditions:
Button becomes invisible/disabled
Maximum number of clicks reached
No change detected in container content (when containerLocator is provided)
import { clickUntilExhausted } from "@intuned/browser";import { BrowserContext, Page } from "playwright";interface Params {}export default async function handler( params: Params, page: Page, context: BrowserContext) { await page.goto("https://sandbox.intuned.dev/load-more"); const loadMoreButton = page.locator("main main button"); // Select the main button in the main content area. // Click until button disappears or is disabled await clickUntilExhausted({ page, buttonLocator: loadMoreButton, maxClicks: 20, }); // Will keep clicking the button until the button disappears or is disabled or the maxClicks is reached.}
import { clickUntilExhausted } from "@intuned/browser";import { BrowserContext, Page } from "playwright";interface Params {}export default async function handler( params: Params, page: Page, context: BrowserContext) { await page.goto("https://sandbox.intuned.dev/load-more"); const loadMoreButton = page.locator("aside button"); // Select the button in the sidebar. const container = page.locator( 'xpath=//*[@id="root"]/div[1]/main/slot/div/aside/div/div/slot/slot' ); // Watch the sidebar container to detect changes. // This will count the elements under the container given before each click and after, if the count is the same, the function will stop. let clickCount = 0; await clickUntilExhausted({ page, buttonLocator: loadMoreButton, containerLocator: container, heartbeat: () => { clickCount++; console.log(`Clicked ${clickCount} times`); }, maxClicks: 30, clickDelay: 0.5, noChangeThreshold: 0, }); // Will keep clicking the button until the button disappears or is disabled or the maxClicks is reached or no more content is loaded.}