happy-dom 使用

使用 happy-dom 生成document

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

import { Window } from 'happy-dom';

// Create a new Window object
const window = new Window();
const domparser = new window.DOMParser();

const inputs = [
'<input aria-autocomplete="list" aria-expanded="false" aria-owns="search-quick-list_b8ff7daf-20e1-4fbb-b38a-e8956b0dea20" autocomplete="off" class="c-navigation-search--input js--navigation-search--input" data-target="#nav-search-static_b8ff7daf-20e1-4fbb-b38a-e8956b0dea20" id="nav-search-static--input_b8ff7daf-20e1-4fbb-b38a-e8956b0dea20" max-num-list="5" name="q" placeholder="搜索物流.dhl" tabindex="0" type="text"/>',
'<input class="c-voc-tracking-bar--input js--tracking-bar--input" data-error-message="请输入您的追踪号码" id="c-voc-tracking-input-box-93feb1c7-e949-4ba9-93ec-e8177ba64bf1" name="tracking-id" required="" type="text"/>',
'<input aria-autocomplete="list" aria-expanded="false" aria-owns="search-quick-list_4a601d43-b0fe-40c3-91e2-8ef1d84d4bf4_mobile" autocomplete="off" class="c-navigation-search--input js--navigation-search--input" data-target="#nav-search-mobile_4a601d43-b0fe-40c3-91e2-8ef1d84d4bf4" id="nav-search-mobile--input_4a601d43-b0fe-40c3-91e2-8ef1d84d4bf4" max-num-list="5" name="q" placeholder="搜索物流.dhl" tabindex="0" type="text"/>',
'<input class="c-voc-tracking-bar--input js--tracking-bar--input" data-error-message="请输入您的追踪号码" id="c-voc-tracking-input-box-4e2f5755-4129-4b60-b9b8-eb013caab702" name="tracking-id" required="" type="text"/>',
'<input class="c-voc-tracking-bar--input js--tracking-bar--input" data-error-message="请输入您的追踪号码" id="c-voc-tracking-input-box-3bbf287b-d43f-4197-8d5f-352ad354a37f" name="tracking-id" required="" type="text"/>',
'<input aria-autocomplete="list" aria-expanded="false" aria-owns="search-quick-list_b8ff7daf-20e1-4fbb-b38a-e8956b0dea20_sticky" autocomplete="off" class="c-navigation-search--input js--navigation-search--input" data-target="#nav-search-sticky_b8ff7daf-20e1-4fbb-b38a-e8956b0dea20" id="nav-search-sticky--input_b8ff7daf-20e1-4fbb-b38a-e8956b0dea20" max-num-list="5" name="q" placeholder="搜索物流.dhl" tabindex="0" type="text"/>',
'<input class="c-voc-tracking-bar--input js--tracking-bar--input" data-error-message="请输入您的追踪号码" id="c-voc-tracking-input-box-18f42330-cd6a-491d-a9b1-211fd95d4f24" name="tracking-id" required="" type="text"/>',
'<input aria-autocomplete="list" aria-expanded="false" aria-owns="search-quick-list_8f3736d6-d68f-45d8-ab69-e317d1abdfc4_mobile" autocomplete="off" class="c-navigation-search--input js--navigation-search--input" data-target="#nav-search-mobile_8f3736d6-d68f-45d8-ab69-e317d1abdfc4" id="nav-search-mobile--input_8f3736d6-d68f-45d8-ab69-e317d1abdfc4" max-num-list="5" name="q" placeholder="搜索物流.dhl" tabindex="0" type="text"/>',
'<input class="c-voc-tracking-bar--input js--tracking--input-field js--tracking-bar--input js--voc-tracking--input-field" data-error-message="请输入您的追踪号码" id="c-tracking--input" name="tracking-id" type="input"/>'
];

const htmlString = inputs.join('\n');

// Parse the HTML string using the DOMParser


const dom = domparser.parseFromString(htmlString, 'text/html');


console.log(dom);

happy-dom 使用
https://kingjem.github.io/2025/02/08/happy-dom/
作者
Ruhai
发布于
2025年2月8日
许可协议