← Back to library
Playwrightnavigationbeginnerv1.20+

Scroll to an element

Scroll an off-screen element into the viewport before asserting or interacting.

1test('scrolls to the pricing section', async ({ page }) => {
2  await page.goto('/landing');
3
4  const pricing = page.getByTestId('pricing-section');
5
6  // scrollIntoViewIfNeeded does nothing if the element is already visible
7  await pricing.scrollIntoViewIfNeeded();
8
9  // toBeInViewport() asserts the element is actually in the visible area
10  await expect(pricing).toBeInViewport();
11  await expect(pricing.getByRole('heading', { name: /pricing/i })).toBeVisible();
12});
#scroll#scrollIntoView#viewport#toBeInViewport