Selection
表示 DOM 树中的一组节点。
Selections 具有与 jQuery 兼容的 API,但有两个注意事项
- CSS 和屏幕布局未被处理,因此像 css() 和 offset() 这样的调用不可用。
- DOM 树是只读的,您不能设置属性或修改节点。
(请注意,DOM 树的只读特性纯粹是为了避免在看似没有实际用途的代码上增加维护负担——如果提出 compelling 的用例,可以轻松实现修改。)
方法 | 说明 |
---|---|
Selection.attr(name) | 获取 Selection 中第一个元素的属性值。 |
Selection.children([selector]) | 获取匹配元素集合中每个元素的子元素,可选择通过选择器过滤。 |
Selection.closest(selector) | 获取与选择器匹配的第一个元素,通过测试元素本身并向上遍历其祖先。 |
Selection.contents() | 获取匹配元素集合中每个元素的子元素,包括文本节点和注释节点。 |
Selection.data([key]) | 返回匹配元素集合中第一个元素的命名数据存储中的值。 |
Selection.each(fn) | 迭代并对每个匹配元素执行函数。 |
Selection.eq(index) | 将匹配元素集合减少到指定索引处的元素。 |
Selection.filter(selector) | 将匹配元素集合减少到与选择器匹配或通过函数测试的元素。 |
Selection.find(selector) | 查找 Selection 的后代,通过选择器过滤。 |
Selection.first() | 将匹配元素集合减少到集合中的第一个元素。 |
Selection.get(index) | 检索与选择器匹配的 Element (k6/html) |
Selection.has(selector) | 将匹配元素集合减少到具有与选择器匹配的后代的元素。 |
Selection.html() | 获取匹配元素集合中第一个元素的 HTML 内容。 |
Selection.is(selector) | 检查当前匹配的元素集合是否与选择器或元素匹配,如果至少有一个元素与给定参数匹配,则返回 true。 |
Selection.last() | 将匹配元素集合减少到集合中的最后一个元素。 |
Selection.map(fn) | 通过函数处理当前匹配集合中的每个 Selection,生成一个包含返回值的新 Array。 |
Selection.nextAll([selector]) | 获取匹配元素集合中每个元素的所有后续同级元素,可选择通过选择器过滤。 |
Selection.next([selector]) | 获取匹配元素集合中每个元素的紧随其后的同级元素。 |
Selection.nextUntil([selector], [filter]) | 获取每个元素的所有后续同级元素,直到但不包括与选择器匹配的元素。 |
Selection.not(selector) | 从匹配元素集合中移除元素。 |
Selection.parent([selector]) | 获取当前匹配元素集合中每个元素的父元素,可选择通过选择器过滤。 |
Selection.parents([selector]) | 获取当前匹配元素集合中每个元素的祖先元素,可选择通过选择器过滤。 |
Selection.parentsUntil([selector], [filter]) | 获取当前匹配元素集合中每个元素的祖先元素,直到但不包括与选择器匹配的元素。 |
Selection.prevAll([selector]) | 获取匹配元素集合中每个元素的所有先前同级元素,可选择通过选择器过滤。 |
Selection.prev([selector]) | 获取匹配元素集合中每个元素的紧随其前的同级元素。 |
Selection.prevUntil([selector], [filter]) | 获取每个元素的所有先前同级元素,直到但不包括与选择器匹配的元素。 |
Selection.serialize() | 将一组表单元素编码为标准 URL 编码表示法字符串以进行提交。 |
Selection.serializeArray() | 将一组表单元素编码为名称和值数组。 |
Selection.serializeObject() | 将一组表单元素编码为一个对象。 |
Selection.size() | 返回 Selection 中的元素数量。 |
Selection.slice(start [, end]) | 将匹配元素集合减少到由索引范围指定的子集。 |
Selection.text() | 获取 Selection 的文本内容。 |
Selection.toArray() | 将 Selection 中包含的所有元素作为数组检索。 |
Selection.val() | 获取匹配元素集合中第一个元素的当前值。 |
示例
import { parseHTML } from 'k6/html';
import http from 'k6/http';
export default function () {
const res = http.get('https://k6.io');
const doc = parseHTML(res.body);
const pageTitle = doc.find('head title').text();
const langAttr = doc.find('html').attr('lang');
}