grafana/k6 · warning

unmarshaling DOM element state: %w

Error message

unmarshaling DOM element state: %w

What it means

DOMElementState is an internal enum (attached/detached/visible/hidden) that k6 marshals to and from quoted JSON strings. UnmarshalJSON fails when the bytes are not a quoted string at all — json.Unmarshal into a string errors on objects, arrays, or numbers. The payload comes from k6's own injected-script machinery, so a user hitting this is almost certainly seeing an internal inconsistency or a corrupted CDP payload, not a script mistake.

Source

Thrown at internal/js/modules/k6/browser/common/frame.go:72

	"detached": DOMElementStateDetached,
	"visible":  DOMElementStateVisible,
	"hidden":   DOMElementStateHidden,
}

// MarshalJSON marshals the enum as a quoted JSON string.
func (s DOMElementState) MarshalJSON() ([]byte, error) {
	buffer := bytes.NewBufferString(`"`)
	buffer.WriteString(domElementStateToString[s])
	buffer.WriteString(`"`)
	return buffer.Bytes(), nil
}

// UnmarshalJSON unmarshals a quoted JSON string to the enum value.
func (s *DOMElementState) UnmarshalJSON(b []byte) error {
	var j string
	err := json.Unmarshal(b, &j)
	if err != nil {
		return fmt.Errorf("unmarshaling DOM element state: %w", err)
	}
	// Note that if the string cannot be found then it will be set to the zero value.
	*s = domElementStateToID[j]
	return nil
}

// Frame represents a frame in an HTML document.
type Frame struct {
	BaseEventEmitter

	ctx         context.Context
	page        *Page
	manager     *FrameManager
	parentFrame *Frame

	childFramesMu sync.RWMutex
	childFrames   map[*Frame]bool

View on GitHub (pinned to 93accf6570)

Solutions

  1. Upgrade k6 so the injected script and Go module versions match
  2. Reproduce with a minimal script and report it to the k6 browser module maintainers
  3. Retry the operation once — transient payload corruption is possible under heavy load
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await page.click('#x'); // actions that consult element state
} catch (e) {
  if (/unmarshaling DOM element state/.test(e.message)) {
    // transient/internal: retry once, then report upstream
  } else throw e;
}

Prevention

When it happens

Trigger: Internal element-state JSON arriving malformed (version skew between the injected script and the Go module); corrupted CDP responses under extreme load; user scripts do not feed this unmarshaller directly.

Common situations: Mixed k6 versions or stale binaries where injected script state shapes changed; exotic pages producing unexpected shapes to injected scripts.

Related errors


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