{"record":{"id":"f1bc5ad749396e49","repo":"microsoft/playwright","slug":"unable-to-adopt-element-handle-from-a-different-do-f1bc5a","errorCode":null,"errorMessage":"Unable to adopt element handle from a different document","messagePattern":"Unable to adopt element handle from a different document","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright-core/src/server/webkit/wkPage.ts","lineNumber":1005,"sourceCode":"\n  async setInputFilePaths(progress: Progress, handle: dom.ElementHandle<HTMLInputElement>, paths: string[]): Promise<void> {\n    const pageProxyId = this._pageProxySession.sessionId;\n    const objectId = handle._objectId;\n    if (this._browserContext._browser?.options.channel === 'webkit-wsl')\n      paths = await progress.race(Promise.all(paths.map(path => translatePathToWSL(path))));\n    await progress.race(Promise.all([\n      this._pageProxySession.connection.browserSession.send('Playwright.grantFileReadAccess', { pageProxyId, paths }),\n      this._session.send('DOM.setInputFiles', { objectId, paths })\n    ]));\n  }\n\n  async adoptElementHandle<T extends Node>(handle: dom.ElementHandle<T>, to: dom.FrameExecutionContext): Promise<dom.ElementHandle<T>> {\n    const result = await this._session.sendMayFail('DOM.resolveNode', {\n      objectId: handle._objectId,\n      executionContextId: (to.delegate as WKExecutionContext)._contextId\n    });\n    if (!result || result.object.subtype === 'null')\n      throw new Error(dom.kUnableToAdoptErrorMessage);\n    return createHandle(to, result.object) as dom.ElementHandle<T>;\n  }\n\n  async inputActionEpilogue(): Promise<void> {\n  }\n\n  async resetForReuse(progress: Progress): Promise<void> {\n  }\n\n  async getFrameElement(frame: frames.Frame): Promise<dom.ElementHandle> {\n    const parent = frame.parentFrame();\n    if (!parent)\n      throw new Error('Frame has been detached.');\n    const context = await parent.mainContext();\n    const result = await this._session.send('DOM.resolveNode', {\n      frameId: frame._id,\n      executionContextId: (context.delegate as WKExecutionContext)._contextId\n    });","sourceCodeStart":987,"sourceCodeEnd":1023,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/server/webkit/wkPage.ts#L987-L1023","documentation":"adoptElementHandle() calls DOM.resolveNode with the source handle's objectId and the destination context id; if the engine cannot resolve the node in the target context (subtype 'null') it throws dom.kUnableToAdoptErrorMessage. This happens when the handle belongs to a different document than the destination frame's context (e.g. across iframes/processes where the node isn't reachable).","triggerScenarios":"Adopting an ElementHandle originating in document A into a context belonging to document B — cross-origin iframe handles, handles from a detached/navigated document, or handles whose underlying node was removed before adoption resolved.","commonSituations":"Passing element handles from one frame into evaluateHandle of another (cross-origin). Reusing a handle after its frame navigated. Cross-process iframe workflows on WebKit where the node is not directly resolvable.","solutions":["Re-query the element inside the destination frame rather than adopting a foreign handle (use a selector/locator scoped to the target frame).","Verify the handle's frame is the same document as the destination context before adopting; refresh stale handles.","For cross-origin iframes, run logic inside the owning frame and pass serializable data out, not the handle."],"exampleFix":"// before\nconst h = await parentFrame.$('#node');\nawait childFrame.evaluate(n => n.remove(), h); // adoption fails across docs\n\n// after\nawait childFrame.$$eval('#node', els => els.forEach(e => e.remove()));","handlingStrategy":"validation","validationCode":"function sameDocument(handle, destFrame) {\n  return handle?.frame === destFrame;\n}\nif (sameDocument(handle, frame)) await frame.evaluate(h => h.remove(), handle);\nelse await frame.$$eval(sel, els => els.forEach(e => e.remove()));","typeGuard":"null","tryCatchPattern":"try { await destFrame.evaluate(fn, foreignHandle); }\ncatch (e) {\n  if (/different document/.test(e.message)) await destFrame.$$eval(sel, els => els.forEach(e => e.remove()));\n  else throw e;\n}","preventionTips":["Re-query elements in the destination frame instead of adopting foreign handles.","Refresh handles after their frame navigates."],"tags":["webkit","element-handle","frames","cross-document"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}