diff --git a/packages/playwright-core/src/client/jsHandle.ts b/packages/playwright-core/src/client/jsHandle.ts index ad03609e40..bb85894870 100644 --- a/packages/playwright-core/src/client/jsHandle.ts +++ b/packages/playwright-core/src/client/jsHandle.ts @@ -19,6 +19,7 @@ import { ChannelOwner } from './channelOwner'; import { parseSerializedValue, serializeValue } from '../protocol/serializers'; import type * as api from '../../types/types'; import type * as structs from '../../types/structs'; +import { isTargetClosedError } from './errors'; export class JSHandle extends ChannelOwner implements api.JSHandle { private _preview: string; @@ -68,7 +69,13 @@ export class JSHandle extends ChannelOwner im } async dispose() { - return await this._channel.dispose(); + try { + await this._channel.dispose(); + } catch (e) { + if (isTargetClosedError(e)) + return; + throw e; + } } async _objectCount() { diff --git a/tests/page/elementhandle-misc.spec.ts b/tests/page/elementhandle-misc.spec.ts index 2be0d10c8a..7224e87779 100644 --- a/tests/page/elementhandle-misc.spec.ts +++ b/tests/page/elementhandle-misc.spec.ts @@ -85,3 +85,12 @@ it('should focus a button', async ({ page, server }) => { await button.focus(); expect(await button.evaluate(button => document.activeElement === button)).toBe(true); }); + +it('should allow disposing twice', async ({ page }) => { + it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/29945' }); + await page.setContent('
39
'); + const element = await page.$('section'); + expect(element).toBeTruthy(); + await element.dispose(); + await element.dispose(); +});