Reactions

A cookie bar is not a step in your flow. Neither is the newsletter modal that opens after eight seconds, the "rate this app" sheet or the chat bubble that slides over the checkout button. They may or may not appear, at no fixed point, and writing a probe for each of them before every click is how a five-line flow becomes fifty. A reaction is the other way round: you describe the interruption once, and the browser deals with it whenever it shows up.

TL;DR
  • AddReaction(match) registers a standing rule: when match appears, click it. AddReactionWith(match, ReactionOpts{On: ...}) (Go) / addReaction(match, { on }) (TS) clicks something else instead, such as the modal's close button.
  • The reaction is carried out by the browser itself, with the same click as Click: scroll, human-like path, pixel check. Nothing round-trips to your process when it fires.
  • When it fires, the action you are running pauses at a safe point (a click before it presses, a fill between two keys), the reaction clicks, and your action carries on. A click blocked by a modal lands because the reaction dismissed the modal meanwhile.
  • One-shot: it fires once and removes itself. ListReactions shows what is still armed, RemoveReaction(id) takes one down.
  • page.AddReaction covers one page. browser.AddReaction covers every page of the browser, including tabs and popups opened later, and fires once on each.
  • Every click is reported as a reactionFired event on the browser's context events, with the page, the outcome and the action it interrupted.
  • A selector is watched for on the whole page, every iframe included, also frames created after you registered it. .In(...) confines it to one frame or one part of the page.

Register once

Register reactions at the start of the flow. From then on they run beside it.

_, _ = page.Navigate(ctx, "https://shop.example", 0)

// The consent banner, wherever it renders — including inside a
// consent-provider iframe.
_, err := page.AddReaction(ctx,
  browserscale.CSS("button#accept-all"))
if err != nil {
  log.Fatal(err)
}

// The flow itself does not mention the banner at all.
_, _ = page.Click(ctx, browserscale.CSS("a.product"))
_, _ = page.Click(ctx, browserscale.CSS("button.add-to-cart"))

match has to be a CSS(...) or JS(...) locator; references and At(...) are rejected, because there is nothing to watch for. By default the match has to be visible, the same as a wait condition — add .Visible(false) to fire on an element that is present but not yet shown.

Where it is watched for follows the same rules as everywhere else: a CSS match means the whole page, a JS match the main document, and .In(...) confines either — to the consent provider's frame with FrameURL(...), to the page's own document with MainFrame(), or to one dialog. The element named with On is looked for in the frame the match was found in, so it takes no container of its own.

Click something else

Often the element that tells you the interruption is there is not the one you want to press. Watch for the modal, click its close button:

_, _ = page.AddReactionWith(ctx,
  browserscale.CSS("#newsletter-modal"),
  browserscale.ReactionOpts{On: browserscale.CSS(".modal-close")},
)

The On / on target is resolved in the frame the match was found in, so a close button inside the same iframe as its modal needs no frame id. Button and ClickCount change the click itself, the same as on ClickWith.

How it fits around your actions

A reaction never fights your script for the pointer. When it fires, it takes the page's input in turn: the action you are running stops at the next safe point, the reaction clicks, and your action picks up where it stopped, within its own timeout.

  • Click, DragBy and DragTo stop before they press. A click that already pressed finishes first.
  • Fill and Type stop between two keys, never in the middle of a composed character, and finish the text afterwards.
  • Clicks at coordinates, MoveTo and the SelectBy… calls are short and finish first; the reaction comes right after.

So the two work together:

  1. Your Click on the checkout button finds it covered by a modal and keeps working its way in.
  2. The reaction sees the modal, the click steps aside, and the reaction clicks the close button.
  3. The click carries on, finds the button exposed and lands.

Without the reaction the same click would end in a ClickError naming the modal as the occluder — which is also how you find out which reactions a site needs.

A few more rules decide when a reaction acts:

  • Captcha solves come first. With captcha auto-solve on, a solve that needs the page takes it before a reaction does.
  • A held button keeps it out. After a ClickWith that only presses (Action: "press"), no reaction fires until the matching release, so a reaction never lands in the middle of a hand-made drag.
  • The page needs the focus. A reaction on a page in the background waits until that page is focused.
  • A person on the live stream goes first. While someone who took over through the live stream moves the pointer, reactions hold back for a moment.
  • A reaction that did not get to click stays armed. If its click was stopped before it pressed, it fires on the next match.

One-shot, and what is still armed

A reaction fires once and removes itself, so a banner that comes back later does not get clicked again by accident. If an interruption can recur, register it again after it fired.

id, _ := page.AddReaction(ctx, browserscale.CSS(".chat-invite .dismiss"))

pending, _ := page.ListReactions(ctx)
for _, r := range pending {
  log.Printf("armed: %s %s%s", r.ReactionID, r.MatchSelector, r.MatchJsExpression)
}

// false if it already fired or never existed.
removed, _ := page.RemoveReaction(ctx, id)
_ = removed

Reactions stay armed across navigations of the page and are torn down when the page or the session ends.

Every page of the browser

A consent banner does not care which tab you are in. Register the reaction on the browser instead of a page, and it is armed on every page that is open now and on every page that opens later — a popup a link opened, a page you created — and fires once on each.

id, err := browser.AddReaction(ctx,
  browserscale.CSS("button#accept-all"))
if err != nil {
  log.Fatal(err)
}

// Fires on this page, and again on the popup this click opens.
_, _ = page.Click(ctx, browserscale.CSS("a.open-offer"))

It behaves like a page reaction on each page, with the same options. The difference is how long it lives: it stays registered until you remove it or the session ends, however often it has fired.

  • browser.ListReactions lists the reactions registered for every page.
  • page.ListReactions lists what is still armed on that page, the browser-wide ones included until they fired there. AllPages / allPages tells them apart.
  • RemoveReaction(id) — on the browser or on any page — takes a browser-wide reaction down everywhere, including for pages that open later.

Seeing what fired

Each click a reaction makes is reported as a reactionFired event on the browser's context events, next to the page and captcha events. It names the page, the reaction id, the frame the match was found in, whether the click was carried out and which of your actions it interrupted, if any. A browser-wide reaction reports once per page it fires on.

listener, err := browser.ContextEvents(ctx, func(ev browserscale.ContextEvent) {
  if ev.Kind != browserscale.ContextEventReactionFired {
      return
  }
  r := ev.Reaction
  if r.Success {
      log.Printf("reaction %s clicked on %s", r.ReactionId, ev.Page.Id())
  } else {
      log.Printf("reaction %s failed: %s", r.ReactionId, r.Error.Code)
  }
})
if err != nil {
  log.Fatal(err)
}
defer listener.Stop()

A click that fails — its target gone, or covered by something it could not get past — is reported with success false and the click's error code, and uses the reaction up like a click that landed. A reaction that was stopped before it pressed reports nothing and stays armed.

Reaction or wait?

Both watch the page for a condition, but they answer different questions:

The thing…Use
may or may not appear, and only needs dismissinga reaction
decides what your flow does next (success, error, challenge)a Wait with one condition per outcome
is the element you are about to act onnothing — the action finds it

Gotchas

  • A JS match watches the main document only. Many consent managers render inside their own iframe. A CSS match finds the button there by itself; an expression has to be put in the frame, or in AnyFrame().
  • Be specific. A reaction on button clicks the first visible button it sees. Match the interruption, not a generic element. This goes double for a browser-wide reaction, which watches every page.
  • Reactions click; they do not type. For an interruption that needs input, wait for it as one of the outcomes and handle it in your flow.
See also