grafana/k6 · error

getting injected script: %w

Error message

getting injected script: %w

What it means

Failure inside evalWithScript, the helper every injected-script-based element method uses (click/fill/selectText/checks/etc.): getInjectedScript could not produce the JSHandle to the InjectedScript instance. The inner error is an evaluation failure in the frame's execution context (context destroyed by navigation, target closed) or the evaluated handle being nil / not a JSHandle (ErrJSHandleInvalid).

Source

Thrown at internal/js/modules/k6/browser/common/element_handle.go:1596

// WaitForSelector waits for the selector to appear in the DOM.
func (h *ElementHandle) WaitForSelector(selector string, opts *FrameWaitForSelectorOptions) (*ElementHandle, error) {
	handle, err := h.waitForSelector(h.ctx, selector, opts)
	if err != nil {
		return nil, fmt.Errorf("waiting for selector %q: %w", selector, err)
	}

	return handle, nil
}

// evalWithScript evaluates the given js code in the scope of this ElementHandle and returns the result.
// The js code can call helper functions from injected_script.js.
func (h *ElementHandle) evalWithScript(
	ctx context.Context,
	opts evalOptions, js string, args ...any,
) (any, error) {
	script, err := h.execCtx.getInjectedScript(h.ctx)
	if err != nil {
		return nil, fmt.Errorf("getting injected script: %w", err)
	}
	return h.eval(ctx, opts, js, append([]any{script}, args...)...)
}

// eval evaluates the given js code in the scope of this ElementHandle and returns the result.
func (h *ElementHandle) eval(
	ctx context.Context,
	opts evalOptions, js string, args ...any,
) (any, error) {
	// passing `h` makes it evaluate js code in the element handle's scope.
	return h.execCtx.eval(ctx, opts, js, append([]any{h}, args...)...)
}

func (h *ElementHandle) newAction(
	states []string, fn elementHandleActionFunc, force bool, retry bool, noWaitAfter bool, timeout time.Duration,
) func(apiCtx context.Context, resultCh chan any, errCh chan error) {
	// All or a subset of the following actionability checks are made before performing the actual action:
	// 1. Attached to DOM

View on GitHub (pinned to 93accf6570)

Solutions

  1. After any action that can navigate, wait for load before reusing handles: page.waitForLoadState() or page.waitForNavigation()
  2. Always re-query element handles after navigation instead of reusing pre-navigation handles
  3. Ensure the page/context is still open before acting (do not close early in the iteration)
  4. Give chromium enough memory/CPU in the environment if targets are crashing

Example fix

// before
page.$('#submit').click();
page.$('.result').textContent(); // context destroyed: getting injected script

// after
page.$('#submit').click();
page.waitForLoadState('load');
page.waitForSelector('.result', { state: 'attached' }).textContent();
Defensive patterns

Strategy: validation

Validate before calling

// never reuse pre-navigation handles: settle the page first
page.waitForLoadState('load');
const el = page.waitForSelector('#target', { state: 'attached' });
el.click();

Try / catch

try {
  el.click();
} catch (e) {
  if (String(e).includes('getting injected script')) {
    // context was destroyed: re-query after load and retry once
    page.waitForLoadState('load');
    page.waitForSelector('#target', { state: 'attached' }).click();
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Calling an element action while the frame is navigating (Runtime.evaluate fails with context destroyed); acting on a handle after page.close()/context.close(); the browser tab crashing; executing against an about:blank frame that has just been swapped.

Common situations: Acting on handles immediately after a click that navigates, without waiting for the new document; parallel VUs sharing a browser context that gets closed by one iteration; OOM-killed chromium in constrained containers.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/e37bb06bcb64ebe1. Report an issue: GitHub.