How do I scroll to the bottom of the page using Playwright (Python)?

In my Playwright tests, I need to scroll all the way to the bottom to load more content (infinite scroll). What are some different ways to do this?

Hey @Punamhans would love to share the easiest method I use is just:

await page.evaluate("window.scrollTo(0, document.body.scrollHeight)")

It scrolls to the bottom of the page instantly. Yep, it’s that easy, hope it helped.

Hey @Punamhans, I think for dynamic content loading, I’ve had better results using a loop:

while True:
    previous_height = await page.evaluate("document.body.scrollHeight")
    await page.mouse.wheel(0, 1000)
    await page.wait_for_timeout(1000)
    new_height = await page.evaluate("document.body.scrollHeight")
    if new_height == previous_height:
        break

Works great for pages that load content in chunks. You can try this approch as well.

Just to think from a different angle here, the use of While loop is very rare @babitakumari, but that’s something I would give a try.

If you’re interacting with a scrollable div instead of the whole window, you can target that element specifically:

scroll_box = page.locator("#scrollable-container")
await scroll_box.evaluate("el => el.scrollTop = el.scrollHeight")

This saved me when testing chat UIs or custom scroll areas.