Skip to main content
async def click_until_exhausted(
    page: Page,
    button_locator: Locator,
    heartbeat: Callable[[], None] = lambda: None,
    *,
    container_locator: Locator | None = None,
    max_clicks: int = 50,
    click_delay: float = 0.5,
    no_change_threshold: int = 0,
)
Repeatedly clicks a button until no new content appears or max clicks reached. This function handles “Load More” pagination patterns by clicking a button repeatedly until one of these conditions is met:
  • The button becomes invisible or disabled
  • No new content is loaded (when container_locator is provided)
  • Maximum number of clicks is reached
The function automatically waits between clicks and handles network settling after each click.

Examples

from intuned_browser import click_until_exhausted
async def automation(page, params, **_kwargs):
    await page.goto("https://example.com/products")
    # Click the "Show More" button exactly 3 times
    await click_until_exhausted(
        page=page,
        button_locator=page.locator("button:has-text('Show More')"),
        max_clicks=3
    )

Arguments

page
Page
required
Playwright Page object
button_locator
Locator
required
Locator for the button to click
heartbeat
Callable[[], None]
Optional callback function invoked after each click. Useful for keeping automation sessions alive. Defaults to lambda: None.
container_locator
Locator
default:"None"
Optional content container used to detect changes. When provided, the function monitors the container’s child count to determine if new content was loaded. Defaults to None.
max_clicks
int
default:"50"
Maximum number of times to click the button. Acts as a safety limit to prevent infinite loops. Defaults to 50.
click_delay
float
default:"0"
Delay in seconds after each click. Allows time for content to load. Defaults to 0.5.
no_change_threshold
int
default:"0"
Minimum change in content size (number of child elements) required to continue clicking. Set to 0 to require any change. Defaults to 0.

Returns: None

Function completes when one of the stopping conditions is met