Why retailer stock checks need a browser
An in stock tracker cannot trust the HTML alone, because retailer product pages usually fetch availability after it arrives, and they resolve it against a store the browser told them about earlier. A plain HTTP GET can return a complete-looking page with no usable stock answer, because the number a customer sees is produced by the page’s own JavaScript.
Two things follow from that. The session matters as much as the URL, and the check has to run somewhere that executes JavaScript and keeps cookies between requests.
The naive version of this pipeline is a script that opens a product URL, greps for “Add to Cart”, and writes a row. It works on the day it is written and produces confident nonsense the first time a page changes its button text, lazy-loads the pickup widget, or refuses the request outright. The rest of this post is about the parts that stop that.
What one check actually is
A check is one SKU at one store at one moment, with enough evidence attached that a human can defend the result. Modelling it as a single boolean is the mistake that makes these systems untrustworthy.
| Field | Example | Why it matters |
|---|---|---|
sku |
style or item number | joins the result to the catalogue |
store_id |
four-digit store number | availability is per store, not per chain |
status |
in stock, out of stock, unknown, blocked | four states, not two |
quantity |
integer or null | “in stock” and “three left” are different signals |
pickup_eta |
date or null | the difference between available now and available Thursday |
checked_at |
ISO 8601 timestamp | makes the row comparable across runs |
source |
browser, data API, site endpoint | lets you compare paths when they disagree |
evidence |
screenshot or HTML snapshot key | lets a human check a disputed row |
The four-state status is the most consequential design decision in the whole system. A page that failed to render is not out of stock, and a page that returned an access-denied screen is not out of stock either. Collapsing those into false produces a report that looks fine and is wrong, which is worse than an openly missing row.
from dataclasses import dataclass
from datetime import datetime
@dataclass(frozen=True)
class Check:
sku: str
store_id: str
retailer: str
status: str # in_stock | out_of_stock | unknown | blocked
quantity: int | None
pickup_eta: str | None
checked_at: datetime
source: str # browser | data_api | site_endpoint
evidence: str | None # snapshot key, not a raw URL
How the pipeline fits together
The system has five stages, and each one fails in a different way.
- Catalogue. A list of SKUs and a list of stores worth checking. Stores usually come from a radius around each dealer or distributor, which is why a short catalogue can turn into thousands of checks.
- Planner. Expands the grid into individual checks, caps how many run per retailer per hour, and spreads the rest across the day so the fleet finishes inside its window.
- Runner. Drives the browser, sets the store context, loads the product page, waits for the availability widget rather than for the page load event, and captures a snapshot.
- Normaliser. Maps what the page showed onto the four statuses, attaches the snapshot key, and rejects results where a required field is missing.
- Differ and report. Compares each result with the last known value for that SKU and store, then writes the workbook and emails it.
The planner is where most of the cost control lives. A grid of SKU × store is quadratic, and the daily window is fixed, so the honest question is not “can we check everything” but “which checks change a decision”. Stores with no recent sales, or a set that already showed out of stock for three weeks, can drop to a weekly cadence without losing much.
How do you keep the browser fleet stable?
Stability comes from making each run start from the same known state: one profile per retailer, one store context at a time, and a clean shutdown that lets the profile flush to disk.
The rules that keep it stable:
- One persistent profile per retailer. Profiles keep the store cookies, the session, and the page’s own cached assets. Sharing one profile across retailers leaks state and makes failures impossible to attribute.
- Set the store once, then verify it. Writing the store’s localisation cookies directly is faster and more reliable than driving a store-picker dialog, but the value has to be read back from the page before the first check, or every row inherits the previous store.
- Low concurrency per retailer. Two browsers per site is plenty for a daily check. More parallel sessions shorten the run and increase the chance that every one of them gets refused.
- Wait on the widget, not the page.
domcontentloadedfires long before a pickup box finishes its own request. The reliable signal is the widget’s own ready state. - Recycle the browser. Restarting the context every few dozen checks keeps memory flat and turns a slow leak into a scheduled restart.
- Close, do not kill. A killed process can leave the profile locked, which then surfaces as a browser refusing to start on the next run.
How do you validate 350 checks before trusting the schedule?
A production probe is a full run against live pages whose results are compared with what a human sees, and it is the only thing that separates “the script ran” from “the numbers are right”.
The probe that signed this pipeline off ran 350 checks against live pages (from the project record). A probe of that shape needs four assertions:
- Completeness. The number of rows written equals the number of checks planned. Missing rows are a planner bug, not a data quirk.
- Known states. At least one store confirmed in stock by hand and one confirmed out, both reading correctly.
- Repeatability. Two runs inside an hour produce the same status for items that did not change. Flapping means the evidence threshold is too loose.
- Timing. The full grid finishes inside the window with headroom, measured per check rather than assumed.
Keep the probe output. It becomes the baseline for every later change to selectors, waits, or the normaliser, and it is the fastest way to answer “did we break it or did the site change”.
Reporting: the spreadsheet is the product
The report is what the client actually opens, so it deserves the same care as the extraction. In this build the output is an Excel workbook delivered by email. In Python, a library such as openpyxl writes the file without Excel installed (openpyxl documentation).
The structure that works:
- Changes first. A sheet with only the rows that differ from the previous run, sorted by retailer and store. This is the sheet people read.
- Everything second. One row per check, stable column order, no merged cells, no formatting that a filter breaks.
- Unknowns third. Blocked and unreadable checks in their own sheet, so a gap in coverage is visible instead of silently absent.
- A run header. Timestamp, check count, success rate, and the window the run covered.
Stable column names matter more than pretty ones. Reports get pasted into other workbooks, and a header renamed between runs breaks every downstream formula without any error message.
Browser or data API?
Both paths produce the same table, and they fail in opposite ways. A browser fails loudly and specifically; a data API fails quietly and consistently.
| Controlled browser | Commercial data API | The page’s own inventory response | |
|---|---|---|---|
| Coverage | whatever the page shows | the vendor’s retailer list and field set | whatever the page’s own front end uses |
| Store-level detail | yes, if the store context is set | varies by vendor and retailer | usually, since the page needs it |
| Fields beyond stock | anything rendered | fixed schema | price, quantity, pickup date if present |
| Failure mode | refuses, breaks, or renders empty | stale, partial, or missing rows | undocumented and free to change |
| Who fixes a break | you | the vendor, on their timeline | you |
| Setup cost | profile, waits, evidence | API key and a mapping | reverse-engineered from the page |
Which should you choose? Start with the browser when the grid is small, the field set is unusual, or the retailers are not on any vendor’s list. Move to a data API when the grid grows past what one fleet can cover inside the window and the vendor carries every field you report on. The third column is the option most teams miss: if the page already fetches a structured inventory response, reading that response from inside the same authenticated page session is often more accurate than parsing the rendered widget, and it keeps the browser only as the session holder. The full trade-off, including when buying wins outright, is in when to stop scraping and buy the data.
What broke and what we changed
The failures below are from the single-retailer deployment that shares this architecture.
A page with an empty widget looked like an out-of-stock item. That checker reported a missing availability box on valid pages. The page was valid and the inventory response behind it was returning data; the React widget had simply never initialised. The error message pointed at the retailer instead of at the checker, which cost the most time. The fix was to stop treating the rendered widget as the source of truth, read the page’s own inventory response instead, and only fall back to the widget when that response is absent.
Headless got refused, and the display mattered. The checker’s notes are direct: headless browsers were blocked, so the run moved to a real Chromium headed against a virtual display. The display had to be tall — 1400×1600 — because the pickup box lazy-renders below the fold of a normal viewport. The address was the open question: the notes require a manual run from the server before trusting the schedule, because a datacenter address can be scored differently from a residential one. When a check was refused, the run recorded blocked and stopped rather than retrying.
The store context had to be set without the picker. Driving a store-picker dialog puts a UI dependency and a failure path before the first check. Writing the localisation cookies directly removed both, and the verification step — reading the store back from the page before the first check — is what stopped a whole run from being attributed to the wrong store.
A mobile-browser variant reset its profile on every run. A provisioning step kept clearing browser data, which recreated the first-run screen and broke the store context before the first page loaded. Preserving the profile between runs fixed it. The lesson generalises: any setup step that resets browser state is a reliability bug, even when it makes the run more repeatable in theory.
Redeploys wiped the history. The run history lived inside the container until it was moved to a mounted volume. A stock checker with no history cannot compute changes, and a change report with one day of data is just a list.
Related
Frequently asked questions
Can you monitor store stock without an API?
Yes, if the retailer publishes per-store availability on a public product page. The check loads that page in a browser, sets the store context, and reads the rendered state or the page's own inventory response. The work is not the single check; it is keeping thousands of them accurate across page changes, store context, and refusals.
How often should a stock check run?
Once a day is enough for most distribution and replenishment decisions, because the people acting on the report work on a daily rhythm. Run more often only where a same-day restock decision depends on it, and expect the cost to scale linearly with frequency. A weekly cadence for slow-moving stores usually beats a second daily run.
Why does a stock checker say out of stock when the site says in stock?
Usually because the store context was wrong or the availability widget had not finished loading when the check read it. Both produce an empty result that looks like a negative. That is why the status field needs an unknown value and why the check should capture evidence, so a disputed row can be re-examined instead of argued about.
Do you need a browser for every check?
No. A browser is needed when availability is produced by client-side code or gated by a store session. When the page exposes a structured inventory response inside that session, the browser only has to establish the session; the checks themselves can read the response. A commercial data API removes the browser entirely for the retailers it covers.
What should a daily stock report contain?
A changes sheet, a full results sheet, an exceptions sheet, and a run header with the timestamp and success rate. The changes sheet is the one people read, and the exceptions sheet is the one that keeps the report honest, because it shows what could not be checked instead of leaving it out.
How do you test a stock-monitoring pipeline?
Run a production probe against live pages and compare it with what a human sees. Assert that every planned check produced a row, that hand-verified in-stock and out-of-stock stores read correctly, that two runs an hour apart agree, and that the full grid finishes inside the window. Keep the probe output as a regression baseline.
Does the same approach work for prices?
Yes, with one addition: price needs a timestamp and a currency, and a page can show several prices for the same item. Competitor price monitoring has the same shape of pipeline but a harder extraction problem, which is why it tends to need versioned extraction recipes and a repair loop.
What is the maintenance cost of a browser-based checker?
Budget for selector and wait changes, session or profile resets, and occasional refusals that need a slower schedule, with most of the work arriving after a site redesign. A checker with evidence snapshots and a probe baseline turns that maintenance into a bounded task instead of an investigation.
