| 351 | return [].slice.call(contextNode.querySelectorAll(css)); |
| 352 | } |
| 353 | function getElementByXpath(xpath, contextNode, doc = document) { |
| 354 | contextNode = contextNode || doc; |
| 355 | try { |
| 356 | const result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); |
| 357 | // 应该总是返回一个元素节点 |
| 358 | return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue; |
| 359 | } catch (err) { |
| 360 | throw new Error(`Invalid xpath: ${xpath}`); |
| 361 | } |
| 362 | } |
| 363 | function getAllElementsByXpath(xpath, contextNode, doc = document) { |
| 364 | contextNode = contextNode || doc; |
| 365 | const result = []; |