> ## Documentation Index
> Fetch the complete documentation index at: https://hyperbrowser.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Ad Blocking

> Learn how to block ads and trackers in your browser sessions

Hyperbrowser's browser instances can automatically block ads and trackers. This improves page load times and reduces detection risk. You can also block trackers and other annoyances like cookie notices.

## Enable Ad Blocking

Set the appropriate options when creating a session:

<CodeGroup>
  ```typescript Node.js theme={null}
  import { Hyperbrowser } from "@hyperbrowser/sdk";
  import { config } from "dotenv";

  config();

  const client = new Hyperbrowser({
    apiKey: process.env.HYPERBROWSER_API_KEY,
  });

  async function main() {
    const session = await client.sessions.create({
      adblock: true,
      trackers: true,
      annoyances: true,
      // You must have trackers set to true to enable blocking annoyances and adblock set to true to enable blocking trackers.
    });

    try {
      console.log("Session Live URL:", session.liveUrl);
      console.log("WS Endpoint:", session.wsEndpoint);
      // ... connect with Playwright/Puppeteer and automate
    } finally {
      await client.sessions.stop(session.id);
    }
  }

  main().catch(console.error);
  ```

  ```python Python theme={null}
  import asyncio
  import os
  from dotenv import load_dotenv
  from hyperbrowser import AsyncHyperbrowser
  from hyperbrowser.models import CreateSessionParams

  # Load environment variables from .env file
  load_dotenv()

  client = AsyncHyperbrowser(api_key=os.getenv("HYPERBROWSER_API_KEY"))


  async def main():
      # Create a session and connect to it using Playwright
      session = await client.sessions.create(
          params=CreateSessionParams(
              adblock=True,
              trackers=True,
              annoyances=True,
          )
      )

      try:
          print(f"Session Live URL: {session.live_url}")
          print(f"WS Endpoint: {session.ws_endpoint}")
          # ... connect with Playwright/Puppeteer and automate
      except Exception as e:
          print(f"Error: {e}")
      finally:
          await client.sessions.stop(session.id)


  # Run the async main function
  if __name__ == "__main__":
      asyncio.run(main())
  ```
</CodeGroup>

## Trackers and Annoyances

When enabled at session creation, Hyperbrowser can block many trackers and filter common annoyances such as cookie prompts and popups.

<CodeGroup>
  ```typescript Node.js theme={null}
  await client.sessions.create({
    adblock: true,
    trackers: true,
    annoyances: true,
  });
  ```

  ```python Python theme={null}
  await client.sessions.create(
      params=CreateSessionParams(
          adblock=True,
          trackers=True,
          annoyances=True,
      )
  )
  ```
</CodeGroup>

<Warning>
  To enable trackers blocking, adblock must be enabled. To enable annoyance blocking, adblock and trackers must both be enabled.
</Warning>

## Automatically Accept Cookies

Some site prompt users for cookies in a particularly intrusive way for scraping. If the `acceptCookies` param is set, then Hyperbrowser will automatically accept cookies on the browsers behalf.

<CodeGroup>
  ```typescript Node.js theme={null}
  await client.sessions.create({
    acceptCookies: true,
  });
  ```

  ```python Python theme={null}
  await client.sessions.create(
      params=CreateSessionParams(
          accept_cookies=True,
      )
  )
  ```
</CodeGroup>

## Next Steps

<CardGroup cols={2}>
  <Card title="Lifecycle Management" icon="rotate" href="/sessions/lifecycle">
    Manage session lifecycle
  </Card>

  <Card title="Profiles" icon="user" href="/sessions/profiles">
    Persist cookies and local storage
  </Card>

  <Card title="Static IPs" icon="map-pin" href="/sessions/static-ips">
    Use dedicated IP addresses
  </Card>

  <Card title="Recordings" icon="video" href="/sessions/recordings">
    Record and replay sessions
  </Card>
</CardGroup>
