菜单
文档breadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow k6/browserbreadcrumb arrow Pagebreadcrumb arrow innerText(selector[, options])
开源

innerText(selector[, options])

警告

请改用基于定位器的 locator.innerText([options])

返回 element.innerText

参数类型默认值描述
selector字符串''用于搜索元素的选择器。如果选择器匹配多个元素,将使用第一个。
options对象null
options.strict布尔值false当为 true 时,调用要求选择器解析为单个元素。如果给定的选择器解析为多个元素,调用将抛出异常。
options.timeout数字30000最大超时时间(毫秒)。传入 0 可禁用超时。默认值会被 BrowserContextPage 上的 setDefaultTimeout 选项覆盖。

返回值

类型描述
Promise<string>一个 Promise,当 fulfilled 时返回元素的内部文本(表示元素句柄及其后代的渲染文本内容)。

示例

JavaScript
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);
}