Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
180
rated 0 times [  186] [ 6]  / answers: 1 / hits: 37785  / 7 Years ago, sat, october 28, 2017, 12:00:00

I'm trying to inject jQuery into my Puppeteer page because document.querySelector doesn't cut it for me:



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;
}


The result is it mostly times out. Does anyone have a solution?


More From » jquery

 Answers
24

I have used page.addScriptTag to inject js files.



...
await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'})
...


page.addScriptTag - documentation



Working example using puppeteer: 0.12.0



import { launch } from 'puppeteer'
(async () => {
const browser = await launch({headless: false});
const page = await browser.newPage();
await page.goto('https://example.com', {waitUntil: 'networkidle'});
await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'});
await page.close();
await browser.close();
})();

[#56080] Thursday, October 26, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
antonb

Total Points: 424
Total Questions: 104
Total Answers: 101

Location: Serbia
Member since Tue, Jul 26, 2022
2 Years ago
;