Skip to main content
Version: 24.38.0

ElementHandle.toElement() 方法

🌐 ElementHandle.toElement() method

将当前句柄转换为给定的元素类型。

🌐 Converts the current handle to the given element type.

语法

🌐 Signature

class ElementHandle {
toElement<K extends keyof HTMLElementTagNameMap | keyof SVGElementTagNameMap>(
tagName: K,
): Promise<HandleFor<ElementFor<K>>>;
}

参数

🌐 Parameters

范围

🌐 Parameter

类型

🌐 Type

描述

🌐 Description

tagName

K

所需元素类型的标签名称。

🌐 The tag name of the desired element type.

返回:

Promise<HandleFor<ElementFor<K>>>

异常

🌐 Exceptions

如果句柄不匹配,会发生错误。句柄不会自动释放。

🌐 An error if the handle does not match. The handle will not be automatically disposed.

示例

🌐 Example

const element: ElementHandle<Element> = await page.$('.class-name-of-anchor');
// DO NOT DISPOSE `element`, this will be always be the same handle.
const anchor: ElementHandle<HTMLAnchorElement> = await element.toElement('a');