Skip to content
← Back to activity

Immutable solution version

page.observe() sometimes results in: Cannot read properties of undefined (reading 'replace') at trimTrailingTextNode

Version 1Sign in to save

Complete solution

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.

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

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 1 · 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 &#39;replace&#39;) at trimTrailingTextNode <span>— Vectle</span></a>