Awaited DOM expression $contentDocument causes js error
I am using $contentDocument to get the location of an iframe which is redirected once it loads but I get as error on the line with await f.$contentDocument.location.href. It sometimes works though without any error.
const frames = await hero.document.getElementsByTagName("iframe");
for (const f of frames) {
const frameSource = await f.$contentDocument.location.href;
}
Error:
TypeError: Cannot read properties of undefined (reading 'id')
at CoreTab.getCoreFrameForMeta (C:\Users\Vihanga\Documents\GitHub\scrape\node_modules\.pnpm\@[email protected]\node_modules\client\lib\CoreTab.ts:238:51)
at C:\Users\Vihanga\Documents\GitHub\scrape\node_modules\.pnpm\@[email protected]\node_modules\client\lib\DomExtender.ts:206:49
at async getAwaitedState (C:\Users\Vihanga\Documents\GitHub\scrape\node_modules\.pnpm\@[email protected]\node_modules\client\lib\SetupAwaitedHandler.ts:105:28)
at async Object.createNodePointer (C:\Users\Vihanga\Documents\GitHub\scrape\node_modules\.pnpm\@[email protected]\node_modules\client\lib\SetupAwaitedHandler.ts:74:54)
at async AwaitedHandler.createNodePointer (C:\Users\Vihanga\Documents\GitHub\scrape\node_modules\.pnpm\@[email protected]\node_modules\files\2-finalized\awaited-dom\base\AwaitedHandler.ts:33:12)
at async NodeFactory.createInstanceWithNodePointer (C:\Users\Vihanga\Documents\GitHub\scrape\node_modules\.pnpm\@[email protected]\node_modules\files\2-finalized\awaited-dom\base\NodeFactory.ts:23:19)
That's an annoying error. There's a high likelihood of frames coming and going, which is likely happening here and just not emitting a meaningful error. Can you try/catch around the $contentDocument line?
Yeah, I will try that thanks
I think I get the same error when I do "await hero.querySelector(iframe).$contentDocument"
/git/hero/build/client/lib/CoreTab.js:156
if (!this.frameEnvironmentsById.has(frameMeta.id)) {
^
TypeError: Cannot read properties of undefined (reading 'id')
at CoreTab.getCoreFrameForMeta (/git/hero/client/lib/CoreTab.ts:238:51)
at /git/hero/client/lib/DomExtender.ts:209:26
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Also if I do hero.getFrameEnvironment(hero.querySelector(iframe)) then it returns null but the frame is still clearly in hero.frameEnvironments so it shouldn't be detached.
Very strange. It only happens sometimes for some iframes, but not always.