句柄
简介
Playwright 可以创建指向页面 DOM 元素或页面内任何其他对象的句柄。这些句柄存在于 Playwright 进程中,而实际对象存在于浏览器中。句柄有两种类型
- JSHandle 用于引用页面中的任何 JavaScript 对象
- ElementHandle 用于引用页面中的 DOM 元素,它具有额外的方法,允许对元素执行操作并断言其属性。
由于页面中的任何 DOM 元素也是一个 JavaScript 对象,因此任何 ElementHandle 也是一个 JSHandle。
句柄用于对页面中的那些实际对象执行操作。您可以在句柄上进行评估,获取句柄属性,将句柄作为评估参数传递,将页面对象序列化为 JSON 等。有关这些方法,请参阅 JSHandle 类 API。
API 参考
以下是获取 JSHandle 的最简单方法。
- 同步
- 异步
js_handle = page.evaluate_handle('window')
# Use jsHandle for evaluations.
js_handle = await page.evaluate_handle('window')
# Use jsHandle for evaluations.
元素句柄
不建议使用 ElementHandle,请改用 Locator 对象和 Web-first 断言。
当需要 ElementHandle 时,建议使用 page.wait_for_selector() 或 frame.wait_for_selector() 方法获取它。这些 API 等待元素被附加和可见。
- 同步
- 异步
# 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
将句柄作为参数
句柄可以传递到 page.evaluate() 和类似的方法中。以下代码段在页面中创建一个新数组,使用数据初始化它,并将指向此数组的句柄返回到 Playwright。然后,它在后续评估中使用该句柄
- 同步
- 异步
# 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()
句柄生命周期
可以使用页面方法(例如 page.evaluate_handle()、page.query_selector() 或 page.query_selector_all())或其框架对应方法 frame.evaluate_handle()、frame.query_selector() 或 frame.query_selector_all() 来获取句柄。一旦创建,除非页面导航或通过 js_handle.dispose() 方法手动处理句柄,否则句柄将保留来自 垃圾回收的对象。
API 参考
- JSHandle
- ElementHandle
- element_handle.bounding_box()
- element_handle.get_attribute()
- element_handle.inner_text()
- element_handle.inner_html()
- element_handle.text_content()
- js_handle.evaluate()
- page.evaluate_handle()
- page.query_selector()
- page.query_selector_all()
Locator vs ElementHandle
我们只建议在极少数情况下使用 ElementHandle,即当您需要在静态页面上执行大量的 DOM 遍历时。对于所有用户操作和断言,请改用定位器。
Locator 和 ElementHandle 之间的区别在于,后者指向特定的元素,而 Locator 捕获如何检索该元素的逻辑。
在下面的示例中,句柄指向页面上的特定 DOM 元素。如果该元素更改文本或被 React 用于渲染完全不同的组件,则句柄仍然指向该非常陈旧的 DOM 元素。这可能会导致意外的行为。
- 同步
- 异步
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 元素将被定位两次。
- 同步
- 异步
locator = page.get_by_text("Submit")
locator.hover()
locator.click()
locator = page.get_by_text("Submit")
await locator.hover()
await locator.click()