句柄 (Handles)
Playwright 可以创建指向页面 DOM 元素或页面内任何其他对象的句柄。这些句柄存在于 Playwright 进程中,而实际对象存在于浏览器中。有两种类型的句柄:
- JSHandle 用于引用页面中的任何 JavaScript 对象
- ElementHandle 用于引用页面中的 DOM 元素,它具有额外的方法,允许对元素执行操作并断言其属性。
由于页面中的任何 DOM 元素也是 JavaScript 对象,因此任何 ElementHandle 也是 JSHandle。
句柄用于对页面中的那些实际对象执行操作。您可以在句柄上执行评估、获取句柄属性、将句柄作为评估参数传递、将页面对象序列化为 JSON 等。有关这些内容和方法,请参阅 JSHandle 类 API。
API reference
这是获取 JSHandle 的最简单方法。
- Sync
- Async
js_handle = page.evaluate_handle('window')
# Use jsHandle for evaluations.
js_handle = await page.evaluate_handle('window')
# Use jsHandle for evaluations.
Element Handles
不建议使用 ElementHandle,请改用 Locator 对象和 Web 优先断言。
当需要 ElementHandle 时,建议使用 page.wait_for_selector(selector, **kwargs) 或 frame.wait_for_selector(selector, **kwargs) 方法获取它。这些 API 等待元素被附加并可见。
- Sync
- Async
# Get the element handle
element_handle = page.wait_for_selector('#box')
# Assert bounding box for the element
bounding_box = element_handle.bounding_box()
assert bounding_box.width == 100
# Assert attribute for the element
class_names = element_handle.get_attribute('class')
assert 'highlighted' in class_names
# Get the element handle
element_handle = page.wait_for_selector('#box')
# Assert bounding box for the element
bounding_box = await element_handle.bounding_box()
assert bounding_box.width == 100
# Assert attribute for the element
class_names = await element_handle.get_attribute('class')
assert 'highlighted' in class_names
Handles as parameters
句柄可以传递给 page.evaluate(expression, **kwargs) 和类似方法。以下代码片段在页面中创建一个新数组,使用数据对其进行初始化,并将指向此数组的句柄返回给 Playwright。然后它在随后的评估中使用该句柄:
- Sync
- Async
# Create new array in page.
my_array_handle = page.evaluate_handle("""() => {
window.myArray = [1];
return myArray;
}""")
# Get current length of the array.
length = page.evaluate("a => a.length", my_array_handle)
# Add one more element to the array using the handle
page.evaluate("(arg) => arg.myArray.push(arg.newElement)", {
'myArray': my_array_handle,
'newElement': 2
})
# Release the object when it's no longer needed.
my_array_handle.dispose()
# Create new array in page.
my_array_handle = await page.evaluate_handle("""() => {
window.myArray = [1];
return myArray;
}""")
# Get current length of the array.
length = await page.evaluate("a => a.length", my_array_handle)
# Add one more element to the array using the handle
await page.evaluate("(arg) => arg.myArray.push(arg.newElement)", {
'myArray': my_array_handle,
'newElement': 2
})
# Release the object when it's no longer needed.
await my_array_handle.dispose()
Handle Lifecycle
可以使用页面方法获取句柄,例如 page.evaluate_handle(expression, **kwargs)、page.query_selector(selector, **kwargs) 或 page.query_selector_all(selector) 或其框架对应方法 frame.evaluate_handle(expression, **kwargs)、frame.query_selector(selector, **kwargs) 或 frame.query_selector_all(selector)。一旦创建,句柄将保留对象以防 垃圾回收,除非页面导航或通过 js_handle.dispose() 方法手动释放句柄。
API reference
- JSHandle
- ElementHandle
- element_handle.bounding_box()
- element_handle.get_attribute(name)
- element_handle.inner_text()
- element_handle.inner_html()
- element_handle.text_content()
- js_handle.evaluate(expression, **kwargs)
- page.evaluate_handle(expression, **kwargs)
- page.query_selector(selector, **kwargs)
- page.query_selector_all(selector)
Locator vs ElementHandle
我们仅建议在需要在静态页面上执行大量 DOM 遍历的极少数情况下使用 ElementHandle。对于所有用户操作和断言,请改用定位器。
Locator 和 ElementHandle 之间的区别在于,后者指向特定元素,而 Locator 捕获如何检索该元素的逻辑。
在下面的示例中,句柄指向页面上的特定 DOM 元素。如果该元素更改文本或被 React 用于渲染完全不同的组件,则句柄仍指向那个非常陈旧的 DOM 元素。这可能会导致意外行为。
- Sync
- Async
handle = page.query_selector("text=Submit")
handle.hover()
handle.click()
handle = await page.query_selector("text=Submit")
await handle.hover()
await handle.click()
使用定位器,每次使用定位器时,都会使用选择器在页面中定位最新的 DOM 元素。因此,在下面的代码片段中,底层 DOM 元素将被定位两次。
- Sync
- Async
locator = page.get_by_text("Submit")
locator.hover()
locator.click()
locator = page.get_by_text("Submit")
await locator.hover()
await locator.click()