Element
表示由 Selection 匹配的 DOM 元素,并提供了检查元素内容的 API。
使用 Selection.get(index) 返回 Element 对象。
Element 对象提供了与 DOM Element API 类似的 API,用于检索元素信息。
方法 | 描述 |
---|---|
selection | 匹配元素的 selection。 |
nodeName | 元素的名称。 |
nodeType | 元素的类型。 |
nodeValue | 元素的值。 |
id | 元素的 id。 |
innerHTML | 是一个表示元素内容标记的 DOMString。 |
textContent | 元素的内容。 |
ownerDocument | Element |
attributes | 属性数组。 |
firstChild | Element |
lastChild | Element |
childElementCount | 子元素的数量。 |
firstElementChild | Element |
lastElementChild | Element |
previousSibling | Element |
nextSibling | Element |
previousElementSibling | Element |
nextElementSibling | Element |
parentElement | Element |
parentNode | Element |
childNodes | Element 数组 |
children | Element 数组 |
classList | 类名数组。 |
className | 类名字符串 |
lang | lang 属性的值。 |
toString | 元素的字符串表示。 |
hasAttribute | 布尔值 |
getAttribute | getAttributeNode |
hasAttributes | 布尔值 |
hasChildNodes | 布尔值 |
isSameNode | 布尔值 |
isEqualNode | 布尔值 |
getElementsByClassName | 返回 Element 数组。 |
getElementsByTagName | 返回 Element 数组。 |
querySelector | 返回与元素相关的指定选择器字符串匹配的第一个 Element |
querySelectorAll | 返回与元素相关的指定选择器字符串匹配的所有 Element |
contains | |
matches | 返回一个布尔值,指示元素是否会由指定的选择器字符串选中 |
namespaceURI | 元素的命名空间 URI。 |
isDefaultNamespace | 返回一个布尔值,指示元素是否具有默认命名空间。 |
此外,Element 根据元素类型的不同,还可以提供更多方法。
AnchorElement:hash, host, hostname, port, username, password, origin, pathname, protocol, relist, search, text。
ButtonElement:form, formAction, formEnctype, formMethod, formNoValidate, formTarget, labels, name, value。
CanvasElement:width, height
DataListElement:options
FieldSetElement:elements, type, form
FormElement:elements, length, method
InputElement:form
LabelElement:control, form
LegendElement:form
LinkElement:relList
MapElement:areas, images
ObjectElement:form
OptionElement:disabled, form, index, label, text, value
OutputElement:value, labels
ProgressElement:max, value, position
ScriptElement:text
SelectElement:form, length, options, selectedOptions, selectedIndex, value
StyleElement:text
TableElement:caption, thead, tbody, tfoot, rows
TableCellElement:cellIndex, colSpan, rowSpan, headers
TableRowElement:cells, colSpan, sectionRowIndex, rowIndex
VideoElement:textTracks
TitleElement:text
示例
import { parseHTML } from 'k6/html';
import { sleep } from 'k6';
export default function () {
const content = `
<dl>
<dt id="term-1">Value term 1</dt>
<dt id="term-2">Value term 2</dt>
</dl>
`;
const sel = parseHTML(content).find('dl').children();
const el1 = sel.get(0);
const el2 = sel.get(1);
console.log(el1.nodeName());
console.log(el1.id());
console.log(el1.textContent());
console.log(el2.nodeName());
console.log(el2.id());
console.log(el2.textContent());
sleep(1);
}
import { parseHTML } from 'k6/html';
import { sleep } from 'k6';
export default function () {
const content = `
\t<a href="http://username:password@example.com:80" rel="prev next" target="_self" type="rare" accesskey="q" hreflang="en-US" media="print">6</a>
`;
const el = parseHTML(content).find('a').get(0);
console.log(el.nodeName());
console.log(el.innerHTML());
console.log(el.host());
console.log(el.hostname());
console.log(el.protocol());
sleep(1);
}