| 552 | return [].slice.call(contextNode.querySelectorAll(css)); |
| 553 | } |
| 554 | function getElementByXpath(xpath, contextNode, doc = document) { |
| 555 | contextNode = contextNode || doc; |
| 556 | try { |
| 557 | const result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); |
| 558 | // 应该总是返回一个元素节点 |
| 559 | return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue; |
| 560 | } catch (err) { |
| 561 | throw new Error(`Invalid xpath: ${xpath}`); |
| 562 | } |
| 563 | } |
| 564 | function getAllElementsByXpath(xpath, contextNode, doc = document) { |
| 565 | contextNode = contextNode || doc; |
| 566 | const result = []; |