Skip to content

Adding items to an eCommerce shopping cart with a JavaScript scenario

This guide shows how to add a product to a shopping cart on an eCommerce product page by combining a Python request with a JavaScript scenario: find the add-to-cart control, click it, navigate to the cart, and return the cart-page response.

OUTPUT FORMATSHTMLJSONMarkdown

1. Find the add-to-cart control

Start on the product page you intend to scrape. In your browser, open Inspect Element and identify a CSS selector that uniquely matches the add-to-cart button or control. A unique match matters: a selector that also matches other elements can click the wrong control. If you need help extracting selectors in Chrome, follow the linked selector tutorial.

2. Build the click-and-navigate scenario

Place that selector in the scenario's click step so it runs after the product page has loaded. The scenario pattern in protected module code0001 is: wait, click the selector, wait, evaluate a navigation to the cart page, then wait again. The waits give the page and the click time to take effect before the next step.

3. Navigate to the cart and return its response

Use an evaluate step to set the browser location to the cart page URL. That makes the returned response come from the cart rather than the product page. Replace the placeholder cart URL with the cart address for your target store.

4. Send the request from Python

Send the scenario with a Python requests call, passing the target product URL and the js_scenario parameter. The worked example in protected module code0002 uses a laptop product page, clicks its add-to-cart button, navigates to the cart, and prints the HTTP status code and response body. Swap in your own product URL, selector, and cart URL; the example product URL may expire. The API key and endpoint origin are owner-supplied configuration, shown here only as placeholders.

Install requests and configure API_KEY and the service origin APP_BASE_URL. This request retains the documented product URL and its matching cart navigation; update both together for another target and confirm its add-to-cart selector. The scenario is submitted as request data.

import os
import requests

options = {'url': 'https://www.amazon.com/ASUS-ROG-Strix-Gaming-Laptop/dp/B0DZZWMB2L',
 'js_scenario': '{"instructions":[{"wait_for_and_click":"#add-to-cart-button"},{"wait":2000},{"evaluate":"window.location.href=\'https://www.amazon.com/cart?ref_=ewc_gtc\'"},{"wait":5000}]}'}
options["api_key"] = os.environ["API_KEY"]
response = requests.get(
    os.environ["APP_BASE_URL"].rstrip("/") + "/api/v1",
    params=options,
)
response.raise_for_status()
print(response.status_code)
print(response.content)

Quick checklist before you run it

Before running, check: the selector uniquely matches the add-to-cart control; the click step follows the initial page load; the evaluate step points at the correct cart URL; the final wait allows the cart to render; and the product URL is current. If the response shows the product page instead of the cart, the navigation step is the first thing to re-check.

Scenario flow at a glance
StepWhat to doWhat it produces
Load product pageWait for the page to settleA ready product page
Click add-to-cartClick the unique selector for the buttonThe item is added to the cart
Go to cartEvaluate navigation to the cart URLThe browser moves to the cart page
Read responseWait, then return the cart-page responseCart contents in the response body

Scenario steps summarized from protected module code0001; exact instruction contents are in that module.

Frequently asked questions

How do I find the right add-to-cart selector?

Open the product page in a browser, use Inspect Element, and locate a selector that uniquely matches the add-to-cart control. That selector is then used by the click step in the scenario.

How does the request return the cart page instead of the product page?

After the click, navigate to the cart page with an evaluate step that sets the location to the cart URL, then wait for that page to load before reading the response.

Can I use the example product URL as-is?

Replace the example product URL with the URL you want to scrape, because a specific example URL may expire. Also replace the placeholder selector and cart URL with values for your target page.