> ## Documentation Index
> Fetch the complete documentation index at: https://stagehand-stg-1784.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Using Multiple Tabs

> Act on multiple tabs with Stagehand

Many modern web applications open new tabs when users click certain buttons or links. Without proper multitab support, automation scripts break when expected content appears in a new tab rather than the current one. Stagehand's multitab capabilities ensure your automations work seamlessly across multitab workflows.

## The Stagehand Page

Stagehand automatically adapts to multitab workflows. The `stagehand.page` object always points to the most recently opened or active tab, ensuring your automations continue working even when new tabs are created.

This means you can continue using familiar patterns:

<CodeGroup>
  ```typescript TypeScript theme={null}
  const page = stagehand.page;
  await page.goto("https://example.com");
  await page.act("click the button that opens a new tab");
  // page now automatically points to the new tab
  await page.extract("get data from new tab");
  ```

  ```python Python theme={null}
  page = stagehand.page
  await page.goto("https://example.com")
  await page.act("click the button that opens a new tab")
  # page now automatically points to the new tab
  await page.extract("get data from new tab")
  ```
</CodeGroup>

<Warning>
  **Important**: [Stagehand Agent](/v2/basics/agent) will always operate on the `stagehand.page`. If you need an agent to work across specific tabs, you'll need to manage page switching manually.
</Warning>

## Manual Page Management

For more control or multitab workflows, you can manage multiple tabs explicitly:

<CodeGroup>
  ```typescript TypeScript theme={null}
  // Create a second page
  await stagehand.context.newPage();
  const pages = stagehand.context.pages();

  const githubPage = pages[0];
  const pythonPage = pages[1];

  // Navigate each page to different repositories
  await githubPage.goto("https://github.com/browserbase/stagehand");
  await pythonPage.goto("https://github.com/browserbase/stagehand-python");

  // Extract data from both pages simultaneously
  const [stagehandStars, stagehandPythonStars] = await Promise.all([
    githubPage.extract("extract the repository stars"),
    pythonPage.extract("extract the repository stars")
  ]);

  console.log(`Stagehand stars: ${stagehandStars}`);
  console.log(`Stagehand-Python stars: ${stagehandPythonStars}`);
  ```

  ```python Python theme={null}
  # Create a second page
  await stagehand.context.new_page()
  pages = stagehand.context.pages()

  github_page = pages[0]
  python_page = pages[1]

  # Navigate each page to different repositories  
  await github_page.goto("https://github.com/browserbase/stagehand")
  await python_page.goto("https://github.com/browserbase/stagehand-python")

  # Extract data from both pages
  stagehand_stars = await github_page.extract("extract the repository stars")
  stagehand_python_stars = await python_page.extract("extract the repository stars")

  print(f"Stagehand stars: {stagehand_stars}")
  print(f"Stagehand-Python stars: {stagehand_python_stars}")
  ```
</CodeGroup>

## Handling Tab Events

You can also listen for tab events to control what happens when new tabs are opened:

<CodeGroup>
  ```typescript TypeScript theme={null}
  const page = stagehand.page;
  await page.goto("https://browserbase.github.io/stagehand-eval-sites/sites/five-tab/");

  // close the new tab after it's opened
  page.on("popup", async () => {
    const newPage = stagehand.context.pages()[1];
    await newPage.close();
  });

  await page.act("click the button to open the other page");

  const page_number = await page.extract("extract the page number");
  console.log(`You're on page ${page_number}`);
  ```

  ```python Python theme={null}
  page = stagehand.page
  await page.goto("https://browserbase.github.io/stagehand-eval-sites/sites/five-tab/")

  # Close the new tab after it's opened
  async def handle_popup():
      new_page = stagehand.context.pages()[1]
      await new_page.close()

  page.on("popup", handle_popup)

  await page.act("click the button to open the other page")

  page_number = await page.extract("extract the page number")
  print(f"You're on page {page_number}")
  ```
</CodeGroup>

## Next Steps

<CardGroup cols={2}>
  <Card title="Orchestrate complex workflows with Agent" icon="robot" iconType="sharp-solid" href="/v2/basics/agent">
    Use `Agent` to autonomously execute multi-step tasks and complex workflows.
  </Card>

  <Card title="Working with iframes" icon="frame" iconType="sharp-solid" href="/v2/best-practices/working-with-iframes">
    Learn best practices for interacting with elements inside iframes.
  </Card>

  <Card title="Browser Configuration" icon="browser" iconType="sharp-solid" href="/v2/configuration/browser">
    Manage browser contexts and sessions for complex automation scenarios.
  </Card>

  <Card title="Logging & Debugging" icon="bug" iconType="sharp-solid" href="/v2/configuration/logging">
    Handle errors gracefully and debug automation issues effectively.
  </Card>
</CardGroup>
