| 366 | return [].slice.call(contextNode.querySelectorAll(css)); |
| 367 | } |
| 368 | function getElementByXpath(xpath, contextNode, doc = document) { |
| 369 | contextNode = contextNode || doc; |
| 370 | try { |
| 371 | const result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); |
| 372 | // 应该总是返回一个元素节点 |
| 373 | return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue; |
| 374 | } catch (err) { |
| 375 | throw new Error(`Invalid xpath: ${xpath}`); |
| 376 | } |
| 377 | } |
| 378 | function getAllElementsByXpath(xpath, contextNode, doc = document) { |
| 379 | contextNode = contextNode || doc; |
| 380 | const result = []; |