grafana/k6 · error

waiting for element state

Error message

waiting for element state %q: %w

What it means

Thrown by the k6 browser module's ElementHandle.waitForElementState(). It wraps failure of the internal waitForElementState call: the element did not reach the requested state before the timeout, the state check evaluation failed, or an invalid/unrecognized state string produced an error from the injected script.

Solutions

  1. Verify the state can actually occur: check the app enables/shows the element after the expected user step — fix the script order if not.
  2. Increase the timeout to cover the app's real async behavior: el.waitForElementState('enabled', { timeout: '60s' }).
  3. Wait on state 'attached' if visibility does not matter for your next action.
  4. Re-acquire the handle if the page rerendered: stale handles fail all state waits.

Example fix

// before
const btn = await page.$('#submit');
await btn.waitForElementState('enabled', { timeout: '5s' }); // validation slower than 5s

// after
const btn = await page.waitForSelector('#submit');
await btn.waitForElementState('enabled', { timeout: '30s' });
await btn.click();
Defensive patterns

Strategy: retry

Validate before calling

const state = await el.state().catch(() => null); // 'visible'|'hidden'|'enabled'|'disabled'|'stable'
if (state === 'disabled') {
  throw new Error('button still disabled; complete the prerequisite step first');
}

Try / catch

try {
  await el.waitForElementState('enabled', { timeout: '30s' });
} catch (e) {
  if (String(e).includes('timed out')) {
    await page.screenshot({ path: 'state-timeout.png' }); // diagnose actual page state
    throw e;
  }
  throw e;
}

Prevention

When it happens

Trigger: await el.waitForElementState('visible' | 'stable' | 'enabled', { timeout }) where the element never becomes that state — e.g., waiting 'enabled' on a button kept disabled by the app, or 'visible' on content behind an unexpanded tab; passing an unsupported state string; element detached during the wait.

Common situations: Waiting on a disabled submit button that only enables after form validation passes; accordion/tab content never shown; SPAs detaching the node during the wait; timeouts set lower than the app's real async delay (skeleton screens, spinners).

Related errors


AI-assisted analysis of grafana/k6@3fcf5388d7 (2026-08-18). Data as JSON: /api/errors/d2c688106843db4b. Report an issue: GitHub.

Appendix: source

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

		return nil, handle.typ(apiCtx, text, KeyboardOptions{})
	}
	typeAction := h.newAction(
		[]string{}, typ, false, withRetry, opts.NoWaitAfter, opts.Timeout,
	)
	if _, err := call(h.ctx, typeAction, opts.Timeout); err != nil {
		return fmt.Errorf("typing text %q: %w", text, err)
	}

	applySlowMo(h.ctx)

	return nil
}

// WaitForElementState waits for the element to reach the given state.
func (h *ElementHandle) WaitForElementState(state string, opts *ElementHandleWaitForElementStateOptions) error {
	_, err := h.waitForElementState(h.ctx, []string{state}, opts.Timeout)
	if err != nil {
		return fmt.Errorf("waiting for element state %q: %w", state, err)
	}

	return nil
}

// 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(

View on GitHub (pinned to 3fcf5388d7)