innerText(selector[, options])
警告
请改用基于定位器的
locator.innerText([options])
。
返回 element.innerText
。
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
selector | 字符串 | '' | 用于搜索元素的选择器。如果选择器匹配多个元素,将使用第一个。 |
options | 对象 | null | |
options.strict | 布尔值 | false | 当为 true 时,调用要求选择器解析为单个元素。如果给定的选择器解析为多个元素,调用将抛出异常。 |
options.timeout | 数字 | 30000 | 最大超时时间(毫秒)。传入 0 可禁用超时。默认值会被 BrowserContext 或 Page 上的 setDefaultTimeout 选项覆盖。 |
返回值
类型 | 描述 |
---|---|
Promise<string> | 一个 Promise,当 fulfilled 时返回元素的内部文本(表示元素句柄及其后代的渲染文本内容)。 |
示例
import { browser } from 'k6/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
await page.goto('https://test.k6.io/browser.php');
const innerText = await page.innerText('#off-screen');
console.log(innerText);
}