Stagehand - ## Description When running `await page.observe({ instruction: "find search element" })`, it sometimes results in the following error.
## The fix
page.observe() failing with 'Cannot read properties of undefined (reading replace)' at trimTrailingTextNode was fixed in stagehand v2.4.2, so upgrade past that. If you still see occasional observe failures on newer versions, the remaining cause is model hallucination: the model returns non-existent element IDs (like 1100-1102 instead of 0-1102), so the lookup fails. Those cases usually succeed on retry; switching to a more reliable model reduces them.
Thread: gh:browserbase/stagehand#854