@@ -265,30 +265,33 @@ export class McpPage implements ContextPage {
265265 }
266266
267267 if ( elementHandles . length ) {
268+ const oldHandles = [ ...this . extraHandles ] ;
268269 this . textSnapshot = await TextSnapshot . create ( this , {
269270 extraHandles : elementHandles ,
270271 } ) ;
271272 response . includeSnapshot ( ) ;
272- }
273273
274- const cdpElementIds : string [ ] = [ ] ;
275- // can this be mapped?
276- for ( const [ index , elementHandle ] of elementHandles . entries ( ) ) {
277- const backendNodeId = await elementHandle . backendNodeId ( ) ;
278- if ( ! backendNodeId ) {
279- logger ( `No backendNodeId for stashed DOM element with index ${ index } ` ) ;
280- cdpElementIds . push ( `stashed-${ index } ` ) ;
281- continue ;
282- }
283- const cdpElementId = this . resolveCdpElementId ( backendNodeId ) ;
284- if ( ! cdpElementId ) {
285- logger ( `Could not get cdpElementId for backend node ${ backendNodeId } ` ) ;
286- cdpElementIds . push ( `stashed-${ index } ` ) ;
287- continue ;
274+ for ( const handle of oldHandles ) {
275+ await handle . dispose ( ) . catch ( e => logger ( 'Failed to dispose old handle' , e ) ) ;
288276 }
289- cdpElementIds . push ( cdpElementId ) ;
290277 }
291278
279+ const cdpElementIds = await Promise . all (
280+ elementHandles . map ( async ( elementHandle , index ) => {
281+ const backendNodeId = await elementHandle . backendNodeId ( ) ;
282+ if ( ! backendNodeId ) {
283+ logger ( `No backendNodeId for stashed DOM element with index ${ index } ` ) ;
284+ return `stashed-${ index } ` ;
285+ }
286+ const cdpElementId = this . resolveCdpElementId ( backendNodeId ) ;
287+ if ( ! cdpElementId ) {
288+ logger ( `Could not get cdpElementId for backend node ${ backendNodeId } ` ) ;
289+ return `stashed-${ index } ` ;
290+ }
291+ return cdpElementId ;
292+ } )
293+ ) ;
294+
292295 const recursivelyReplaceStashedElements = ( node : unknown ) : unknown => {
293296 if ( Array . isArray ( node ) ) {
294297 return node . map ( x => recursivelyReplaceStashedElements ( x ) ) ;
0 commit comments