I cannot figure out why a snippet I run in Chrome Dev Tools works perfectly, but I can't get it to work in Puppeteer. As an experiment, I was trying to "drive" the canvas for the NY Times Letter Boxed game: https://www.nytimes.com/puzzles/letter-boxed
In Google Chrome, I can get a handle to the canvas functions by selecting the canvas element and then extracting the "__reactInternalInstance" property from it (usually the property is named something like "__reactInternalInstance$2c4oug63f7m"). The javascript that does it is here:
var myObject = {};
var mycanvas = document.querySelector("#pz-game-root > div > div > div.lb-square-container > canvas")
for (let property in mycanvas) {
if (property.includes("__reactInternalInstance")) {
console.log(`${property}`);
myObject = mycanvas[`${property}`];
}
}
console.log(myObject);
//at this point IN CHROME,
//myObject has access to some of the interactive functions of the game play
Nearly identical code in puppeteer cannot seem to get access to any canvas properties, including the "__reactInternalInstance" property.
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
//args: ["--renderer","--single-process"],
executablePath: "/usr/bin/google-chrome",
headless: true,
userDataDir: '/tmp'
});
const page = await browser.newPage();
//use a mobile user agent
page.setUserAgent("Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Mobile Safari/537.36");
await page.setViewport({ width: 412, height: 915 });
await page.goto('https://www.nytimes.com/puzzles/letter-boxed');
try {
/**
* click the start button on the splash screen
*/
await page.waitForSelector("button.pz-moment__button.primary");
let startButton = await page.$("button.pz-moment__button.primary");
await startButton.click();
await delay(3000);
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* myHandle object comes back null,
* no properties are logged,
* and the "Found the canvas" message doesn't print
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
//capture the canvas element
await page.waitForSelector("#pz-game-root > div > div > div.lb-square-container > canvas");
let myHandle = await page.$("#pz-game-root > div > div > div.lb-square-container > canvas",
(canv) => {
console.log("Found the canvas");
for (let property in canv) {
console.log(`${property}`);
}
return canv;
}
);
console.log(myHandle);
} catch (error) {
console.log("Failure occurred");
console.log(error);
}
await browser.close();
})();
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
I'm not very proficient with Puppeteer. Anyone able to tell me what I'm doing wrong?