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

> ## Agent Instructions
> The Zenrows API (Fetch, Extract via extract=auto, and Batch) is described in OpenAPI 3.1 at https://www.zenrows.com/openapi.json and listed in the RFC 9727 API catalog at https://www.zenrows.com/.well-known/api-catalog. Use the spec for exact parameter names and types.

# Lesson 5: Add a product to the cart with Browser

> Foundations lesson 5: use Zenrows Browser to choose a product's options, add it to the cart, read the total and take a screenshot.

Lessons 1 to 4 read pages. Some jobs need you to act on a page: pick options, click buttons, and follow the result to the next page. For that you drive a real browser. In this lesson you add a hoodie to the demo store's cart and check what the cart says.

**What you'll build:** `add_to_cart.py`, a Playwright script that runs in Zenrows Browser, the browser for agents. It opens a product, chooses a size and a color, adds the product to the cart, reads the cart total and saves a screenshot.

**What you'll learn:**

* How to connect Playwright to Browser with one WebSocket URL.
* How to select options, click and wait for the page to respond.
* How to keep a browser session short and always close it.

**Time:** about 10 minutes.

## When to use Browser

Fetch returns a page; Browser gives you a live, cloud-hosted Chrome that you control step by step over the Chrome DevTools Protocol (CDP). For when each one fits, see [Choose the right Zenrows primitive](/academy/choose-a-primitive#read-or-act).

## Install Playwright

You use Playwright's own Python package. Browser runs Chrome in the cloud, so there's no local browser to download:

```bash theme={"dark"}
pip install playwright
```

## Write the cart script

Create `add_to_cart.py`:

```python Python (Playwright) theme={"dark"}
from playwright.sync_api import sync_playwright

ZENROWS_API_KEY = "YOUR_ZENROWS_API_KEY"
CONNECTION_URL = f"wss://browser.zenrows.com?apikey={ZENROWS_API_KEY}"
PRODUCT_URL = "https://www.scrapingcourse.com/ecommerce/product/abominable-hoodie/"
CART_URL = "https://www.scrapingcourse.com/ecommerce/cart/"

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp(CONNECTION_URL)
    try:
        context = browser.contexts[0] if browser.contexts else browser.new_context()
        page = context.new_page()
        page.set_default_timeout(60_000)

        page.goto(PRODUCT_URL, wait_until="domcontentloaded")
        print("Opened:", page.locator("h1.product_title").inner_text())

        page.select_option("select#size", "M")
        page.select_option("select#color", "Blue")
        page.click("button.single_add_to_cart_button")
        message = page.locator(".woocommerce-message")
        message.wait_for()
        print("Store says:", " ".join(message.inner_text().split()))

        page.goto(CART_URL, wait_until="domcontentloaded")
        total = page.locator(".wc-block-components-totals-footer-item .wc-block-components-totals-item__value")
        total.wait_for()
        item = page.locator(".wc-block-cart-item__product .wc-block-components-product-name").first
        print("In the cart:", item.inner_text())
        print("Cart total:", total.inner_text())

        page.screenshot(path="cart.png", full_page=True)
        print("Saved cart.png")
    finally:
        browser.close()
```

Here is what each part does:

<Steps>
  <Step title="Connect">
    `connect_over_cdp()` opens a Browser session at `wss://browser.zenrows.com` with your API key. From here on, `page` is a normal Playwright page running in the cloud.
  </Step>

  <Step title="Open the product">
    `wait_until="domcontentloaded"` continues as soon as the page's HTML is ready, without waiting for every analytics script. The 60-second default timeout gives each step room on slower pages.
  </Step>

  <Step title="Choose options and add to cart">
    `select_option()` picks the size and the color, the same `select#size` and `select#color` dropdowns you read with CSS selectors in Lesson 2. The click submits the form, and `message.wait_for()` waits for the store's confirmation.
  </Step>

  <Step title="Read the cart">
    The cart page renders its contents with JavaScript, so the script waits for the total to appear before it reads it. The cart remembers the hoodie because the session keeps the store's cookies.
  </Step>

  <Step title="Close the session">
    `browser.close()` sits in a `finally` block, so the session ends even when a step fails. Closing the session ends its billing.
  </Step>
</Steps>

## Run it

```bash theme={"dark"}
python add_to_cart.py
```

The whole session takes about 30 seconds:

```text theme={"dark"}
Opened: Abominable Hoodie
Store says: View cart “Abominable Hoodie” has been added to your cart.
In the cart: Abominable Hoodie
Cart total: $69.00
Saved cart.png
```

Open `cart.png`: it shows the cart with one Abominable Hoodie, size M, color Blue, and a total of \$69.00.

## Make it your own

| Change | How |
| - | - |
| Browse from a specific country | Add `&proxy_country=us` to the connection URL, see [Country](/browser-sessions/features/country) |
| Set how long the session lasts | Add `&session_ttl=2m` to the connection URL, see [Session TTL](/browser-sessions/features/session-ttl) |
| Use the async API or Node.js | See [Playwright with Browser](/browser-sessions/get-started/playwright) |
| Use Puppeteer instead | See [Puppeteer with Browser](/browser-sessions/get-started/puppeteer) |

## Ask your AI agent

With the [Zenrows MCP server](/mcp/overview) connected to Claude, Cursor or any MCP client, paste this prompt. Your agent picks the right Zenrows tools and settings on its own.

```text theme={"dark"}
Use the Zenrows browser to open https://www.scrapingcourse.com/ecommerce/product/abominable-hoodie/,
choose size M and color Blue, and add it to the cart. Then open the cart, tell me the total,
take a screenshot, and close the browser session.
```

## What you built

`add_to_cart.py` completes a real shopping flow in a cloud browser. You can:

* Connect Playwright to Zenrows Browser with `connect_over_cdp()` and one URL.
* Select options, click, and wait for the page to respond before you read it.
* Keep sessions short and close them in a `finally` block.

## Next lesson

<Card title="Lesson 6: Run the tracker every day" icon="arrow-right" href="/academy/foundations/run-on-a-schedule">
  Turn the Batch job into a daily schedule and get notified when each run finishes.
</Card>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.