【发布时间】:2018-04-09 19:05:39
【问题描述】:
我正在尝试将 jQuery 注入我的 Puppeteer 页面,因为 document.querySelector 不适合我:
async function inject_jquery(page){
await page.evaluate(() => {
var jq = document.createElement("script")
jq.src = "https://code.jquery.com/jquery-3.2.1.min.js"
document.querySelector("head").appendChild(jq)
})
const watchDog = page.waitForFunction('window.jQuery !== undefined');
await watchDog;
}
结果主要是超时。有人有解决办法吗?
【问题讨论】:
-
你试过
document.getElementsByTagName('head')[0].appendChild(jq)吗?此外,由于您正在设置src,因此侦听脚本元素的load事件然后返回可能会更加健壮。
标签: javascript jquery node.js google-chrome-devtools puppeteer