| 562 | } |
| 563 | } |
| 564 | function getAllElementsByXpath(xpath, contextNode, doc = document) { |
| 565 | contextNode = contextNode || doc; |
| 566 | const result = []; |
| 567 | try { |
| 568 | const query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); |
| 569 | for (let i = 0; i < query.snapshotLength; i++) { |
| 570 | const node = query.snapshotItem(i); |
| 571 | // 如果是 Element 节点 |
| 572 | if (node.nodeType === 1) result.push(node); |
| 573 | } |
| 574 | } catch (err) { |
| 575 | throw new Error(`无效 Xpath: ${xpath}`); |
| 576 | } |
| 577 | return result; |
| 578 | } |
| 579 | function getOneElements(selector, contextNode = undefined, doc = document) { |
| 580 | if (!selector) return; |
| 581 | contextNode = contextNode || doc; |