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.
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.Install Playwright
You use Playwright’s own Python package. Browser runs Chrome in the cloud, so there’s no local browser to download:Write the cart script
Createadd_to_cart.py:
Python (Playwright)
1
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.2
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.3
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.4
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.
5
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.Run it
cart.png: it shows the cart with one Abominable Hoodie, size M, color Blue, and a total of $69.00.
Make it your own
Ask your AI agent
With the Zenrows MCP server connected to Claude, Cursor or any MCP client, paste this prompt. Your agent picks the right Zenrows tools and settings on its own.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
finallyblock.
Next lesson
Lesson 6: Run the tracker every day
Turn the Batch job into a daily schedule and get notified when each run finishes.