Solution skill
page.observe() sometimes results in: Cannot read properties of undefined (reading 'replace') at trimTrailingTextNode
Complete solution
Shows how to fix page.observe() sometimes results in: Cannot read properties of undefined (reading 'replace') at trimTrailingTextNode. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
page.observe() failing with 'Cannot read properties of undefined (reading replace)' at trimTrailingTextNode was fixed in stagehand v2.4.2, so upgrade past that.
Steps
- 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.
When to use
You are seeing this: Stagehand - ## Description When running await page.observe({ instruction: "find search element" }), it sometimes results in the following error. Use this skill when you run into "page.observe() sometimes results in: Cannot read properties of undefined (reading 'replace') at trimTrailingTextNode".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
Versions mentioned in the source: stagehand v2.4.2. If you are on something much newer or older, the details may have shifted.
Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
Sources
- https://github.com/browserbase/stagehand/issues/854
Evidence
In gh:browserbase/stagehand#854 (https://github.com/browserbase/stagehand/issues/854), the issue reported: ## Description When running await page.observe({ instruction: "find search element" }), it sometimes results in the following error. ``` trace: "TypeError: Cannot read properties of undefined (reading 'replace') at trimTrailingTextNode (./nodemodules/@browserbasehq/stagehand/dist/index.js:2330:16) at StagehandObserveHandler.[anonymous] (./nodemodules/@browserbasehq/stagehand/dist/index.js:3328:32) at Generator.next ([anonymous]) at ./nodemodules/@browserbasehq/stagehand/dist/index.js:79:61 at new Promise ([anonymous]) at async (./nodemodules/@browserbasehq/stagehand/dist/index. The verified answer: 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.
By @issueatlas · Version 2 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_fkuBSQZEqqkkeYHAD3t_pg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">page.observe() sometimes results in: Cannot read properties of undefined (reading 'replace') at trimTrailingTextNode <span>— Vectle</span></a>