Skip to main content

JSHandle

JSHandle 表示页内 JavaScript 对象。JSHandles 可以使用 page.evaluate_handle() 方法创建。

¥JSHandle represents an in-page JavaScript object. JSHandles can be created with the page.evaluate_handle() method.

window_handle = page.evaluate_handle("window")
# ...

JSHandle 会阻止引用的 JavaScript 对象被垃圾回收,除非该句柄通过 js_handle.dispose() 公开。当导航其原始框架或父上下文被破坏时,JSHandles 会自动处置。

¥JSHandle prevents the referenced JavaScript object being garbage collected unless the handle is exposed with js_handle.dispose(). JSHandles are auto-disposed when their origin frame gets navigated or the parent context gets destroyed.

JSHandle 实例可以用作 page.eval_on_selector()page.evaluate()page.evaluate_handle() 方法中的参数。

¥JSHandle instances can be used as an argument in page.eval_on_selector(), page.evaluate() and page.evaluate_handle() methods.


方法

¥Methods

dispose

Added before v1.9 jsHandle.dispose

jsHandle.dispose 方法停止引用元素句柄。

¥The jsHandle.dispose method stops referencing the element handle.

用法

¥Usage

js_handle.dispose()

返回

¥Returns


evaluate

Added before v1.9 jsHandle.evaluate

返回 expression 的返回值。

¥Returns the return value of expression.

此方法将此句柄作为第一个参数传递给 expression

¥This method passes this handle as the first argument to expression.

如果 expression 返回 Promise,那么 handle.evaluate 将等待 Promise 解析并返回其值。

¥If expression returns a Promise, then handle.evaluate would wait for the promise to resolve and return its value.

用法

¥Usage

tweet_handle = page.query_selector(".tweet .retweets")
assert tweet_handle.evaluate("node => node.innerText") == "10 retweets"

参数

¥Arguments

要在浏览器上下文中执行的 JavaScript 表达式。如果表达式的计算结果为函数,则会自动调用该函数。

¥JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.

传递给 expression 的可选参数。

¥Optional argument to pass to expression.

返回

¥Returns


evaluate_handle

Added before v1.9 jsHandle.evaluate_handle

expression 的返回值作为 JSHandle 返回。

¥Returns the return value of expression as a JSHandle.

此方法将此句柄作为第一个参数传递给 expression

¥This method passes this handle as the first argument to expression.

jsHandle.evaluatejsHandle.evaluateHandle 之间的唯一区别是 jsHandle.evaluateHandle 返回 JSHandle

¥The only difference between jsHandle.evaluate and jsHandle.evaluateHandle is that jsHandle.evaluateHandle returns JSHandle.

如果传递给 jsHandle.evaluateHandle 的函数返回 Promise,那么 jsHandle.evaluateHandle 将等待 Promise 解析并返回其值。

¥If the function passed to the jsHandle.evaluateHandle returns a Promise, then jsHandle.evaluateHandle would wait for the promise to resolve and return its value.

详细信息请参见 page.evaluate_handle()

¥See page.evaluate_handle() for more details.

用法

¥Usage

js_handle.evaluate_handle(expression)
js_handle.evaluate_handle(expression, **kwargs)

参数

¥Arguments

要在浏览器上下文中执行的 JavaScript 表达式。如果表达式的计算结果为函数,则会自动调用该函数。

¥JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.

传递给 expression 的可选参数。

¥Optional argument to pass to expression.

返回

¥Returns


get_properties

Added before v1.9 jsHandle.get_properties

该方法返回一个映射,其中自己的属性名称作为键,JSHandle 实例作为属性值。

¥The method returns a map with own property names as keys and JSHandle instances for the property values.

用法

¥Usage

handle = page.evaluate_handle("({ window, document })")
properties = handle.get_properties()
window_handle = properties.get("window")
document_handle = properties.get("document")
handle.dispose()

返回

¥Returns


get_property

Added before v1.9 jsHandle.get_property

从引用的对象中获取单个属性。

¥Fetches a single property from the referenced object.

用法

¥Usage

js_handle.get_property(property_name)

参数

¥Arguments

  • property_name str#

获得的属性

¥property to get

返回

¥Returns


json_value

Added before v1.9 jsHandle.json_value

返回对象的 JSON 表示形式。如果对象有 toJSON 函数,则不会调用它。

¥Returns a JSON representation of the object. If the object has a toJSON function, it will not be called.

注意

如果引用的对象不可字符串化,该方法将返回一个空的 JSON 对象。如果对象有循环引用,则会抛出错误。

¥The method will return an empty JSON object if the referenced object is not stringifiable. It will throw an error if the object has circular references.

用法

¥Usage

js_handle.json_value()

返回

¥Returns


属性

¥Properties

as_element

Added before v1.9 jsHandle.as_element

如果对象句柄是 ElementHandle 的实例,则返回 null 或对象句柄本身。

¥Returns either null or the object handle itself, if the object handle is an instance of ElementHandle.

用法

¥Usage

js_handle.as_element()

返回

¥Returns