JSHandle
JSHandle 表示页面内的 JavaScript 对象。可以使用 Page.evaluateHandle() 方法创建 JSHandle。
🌐 JSHandle represents an in-page JavaScript object. JSHandles can be created with the Page.evaluateHandle() method.
JSHandle windowHandle = page.evaluateHandle("() => window");
// ...
JSHandle 会阻止被引用的 JavaScript 对象被垃圾回收,除非通过 JSHandle.dispose() 显式释放该句柄。当 JSHandle 的源框架被导航或父上下文被销毁时,JSHandle 会自动释放。
🌐 JSHandle prevents the referenced JavaScript object being garbage collected unless the handle is exposed with JSHandle.dispose(). JSHandles are auto-disposed when their origin frame gets navigated or the parent context gets destroyed.
JSHandle 实例可以作为参数用于 Page.evalOnSelector()、Page.evaluate() 和 Page.evaluateHandle() 方法。
🌐 JSHandle instances can be used as an argument in Page.evalOnSelector(), Page.evaluate() and Page.evaluateHandle() methods.
方法
🌐 Methods
asElement
Added before v1.9如果对象句柄是 ElementHandle 的实例,则返回 null 或对象句柄本身。
🌐 Returns either null or the object handle itself, if the object handle is an instance of ElementHandle.
用法
JSHandle.asElement();
返回
dispose
Added before v1.9jsHandle.dispose 方法停止引用元素句柄。
🌐 The jsHandle.dispose method stops referencing the element handle.
用法
JSHandle.dispose();
返回
evaluate
Added before v1.9返回 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.
用法
ElementHandle tweetHandle = page.querySelector(".tweet .retweets");
assertEquals("10 retweets", tweetHandle.evaluate("node => node.innerText"));
参数
-
将在浏览器上下文中求值的 JavaScript 表达式。如果表达式求值为一个函数,该函数将被自动调用。
-
argEvaluationArgument (optional)#可选参数,传递给 expression。
返回
evaluateHandle
Added before v1.9将 expression 的返回值作为 JSHandle 返回。
🌐 Returns the return value of expression as a JSHandle.
此方法将此句柄作为第一个参数传递给 expression。
🌐 This method passes this handle as the first argument to expression.
jsHandle.evaluate 和 jsHandle.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.evaluateHandle()。
🌐 See Page.evaluateHandle() for more details.
用法
JSHandle.evaluateHandle(expression);
JSHandle.evaluateHandle(expression, arg);
参数
-
将在浏览器上下文中求值的 JavaScript 表达式。如果表达式求值为一个函数,该函数将被自动调用。
-
argEvaluationArgument (optional)#可选参数,传递给 expression。
返回
getProperties
Added before v1.9该方法返回一个映射,其键为自身属性名,值为属性值对应的 JSHandle 实例。
🌐 The method returns a map with own property names as keys and JSHandle instances for the property values.
用法
JSHandle handle = page.evaluateHandle("() => ({ window, document })");
Map<String, JSHandle> properties = handle.getProperties();
JSHandle windowHandle = properties.get("window");
JSHandle documentHandle = properties.get("document");
handle.dispose();
返回
getProperty
Added before v1.9从引用的对象中获取单个属性。
🌐 Fetches a single property from the referenced object.
用法
JSHandle.getProperty(propertyName);
参数
返回
jsonValue
Added before v1.9返回对象的 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.
用法
JSHandle.jsonValue();
返回