User Agents

Web browsers and everyday web tech, explained

How to Click a Tab Button with Playwright and Verify It Switched

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Clicking a tab button with Playwright and verifying the correct tab has activated is a common task when testing web applications with dynamic content or navigation interfaces. This process ensures that UI interactions function as intended and that content updates accordingly when users switch between tabs. Using Playwright’s reliable element selection and assertion capabilities, you can simulate a click and confirm visibility or state changes tied to the selected tab. Before getting started, make sure your Playwright environment is set up and you're targeting a web app with accessible tab elements—typically buttons or links with ARIA roles like role="tab".

What You'll Need

  • A working Playwright test environment (Node.js with playwright installed)
  • A web page with tabbed navigation (e.g., buttons with role="tab" or similar)
  • Access to the page’s URL or local server during testing
  • Basic knowledge of CSS selectors or text-based locators
  • A code editor to write and run tests

Step-by-Step

  1. Launch the browser and navigate to the target page
    Start by launching a browser instance and going to the page containing the tab buttons. Use await page.goto('URL') to load the page. Ensure the page has fully loaded by waiting for network idle or a specific element.
  2. Locate the tab button using a reliable selector
    Identify the tab you want to click. Use Playwright’s powerful selector engine: you can select by text (e.g., getByText('Settings')), by role (e.g., getByRole('tab', { name: 'Profile' })), or by CSS class/ID. Prefer ARIA roles or unique text content for stability.
  3. Click the tab button
    Use the click() method on the located element. For example: await page.getByRole('tab', { name: 'History' }).click();. This simulates a real user interaction.
  4. Wait for the tab content to update
    After clicking, wait for the associated content to appear or change. Use waitForSelector(), waitForFunction(), or Playwright’s auto-waiting features. For example, wait for a section with role="tabpanel" to become visible or contain expected text.
  5. Verify the tab is active
    Check that the clicked tab now has an active state. This is often indicated by aria-selected="true", a CSS class like active or selected, or a visual change. Use toHaveAttribute() or toHaveClass() in assertions. Example: await expect(tabElement).toHaveAttribute('aria-selected', 'true');.
  6. Confirm associated content is displayed
    Ensure the correct content panel is visible. Target the corresponding tabpanel (often via getByRole('tabpanel') or a data attribute) and assert it is visible using toBeVisible(). For example: await expect(page.getByRole('tabpanel', { name: 'History' })).toBeVisible();.
  7. Optional: Verify previous tab is inactive
    If multiple tabs exist, confirm the previously active tab is no longer selected. Re-select the old tab element and assert it lacks the active state (e.g., aria-selected="false" or missing active class).
  8. Run and debug the test
    Execute your test script using npx playwright test. If it fails, use Playwright’s trace viewer (npx playwright show-trace) to inspect the page state at each step.

Common Mistakes

One frequent error is using unstable selectors, such as relying on dynamic classes or vague text that may change. Always prefer ARIA roles or unique, static identifiers. Another mistake is not waiting long enough for content to load after the click—Playwright auto-waits for actionability, but dynamic content may require explicit waits. Some developers forget to verify both the tab state and the associated content, leading to false positives. Also, clicking elements that are not interactable (e.g., hidden or disabled) without first checking visibility can cause failures. Lastly, assuming all tabs use the same pattern—some may rely on client-side routing instead of ARIA tabs, requiring different verification logic.

Tips

To improve reliability, use Playwright’s getByRole() method with name and selected state for both selection and assertion—it aligns with accessibility standards and is less likely to break. Wrap tab interactions in reusable functions if testing multiple tabs. Use page.waitForTimeout() sparingly; prefer explicit waits for elements. Leverage Playwright’s test generator (npx playwright codegen) to record the tab interaction and auto-generate selectors. Always test edge cases, like clicking the same tab twice or navigating out of order. Run tests in headed mode initially to visually confirm behavior.

Troubleshooting

If the tab click does not register, first check that the element is visible and enabled using isVisible() and isEnabled(). If the click succeeds but content doesn’t update, inspect network activity—some tabs load data asynchronously. Use page.waitForResponse() if API calls are expected. If assertions fail, verify the exact attribute or class name using browser DevTools. The issue might be a typo or dynamic class variation. If Playwright times out, increase the timeout in the assertion or use waitForSelector with a longer timeout option. If tests pass locally but fail in CI, ensure the viewport size matches and no JavaScript errors occur in headless mode. Use console.log or Playwright’s debug mode (PWDEBUG=1) to step through the test.

Frequently Asked Questions

Can Playwright click tabs that use client-side routing?

Yes, but you may need to wait for a URL change or new content instead of ARIA attributes. Use page.waitForURL() or wait for specific elements to appear.

How do I handle tabs without ARIA roles?

Use text content, CSS classes, or data attributes to locate and verify them. Ensure selectors are unique and stable across updates.

What if the tab content is lazy-loaded?

Wait for network responses or specific elements inside the tab panel using waitForSelector or waitForResponse.

Can I test keyboard navigation for tabs?

Yes—use page.keyboard.press('Tab') and arrow keys. Combine with assertions to verify focus and selection changes.

Why does my tab click work manually but fail in Playwright?

This often means the element is obscured, not in view, or requires a different interaction (e.g., hover first). Use scrollIntoViewIfNeeded() or check for overlays.

Should I use click() or dispatchEvent()?

Use click() for realism. Only use dispatchEvent() if the event handler doesn’t respond to real clicks, which may indicate a testing anti-pattern.