Using Puppeteer I'm trying to navigate to a page and then take a screenshot of an element on the page.
const page = await browser.newPage();
await page.goto('some-url');
const myElement = await page.waitForSelector('.my-class', { visible: true });
const myElementHtml = await page.evaluate((el) => el.innerHTML, myElement);
console.log(myElementHtml); // this contains everything I expect
const myElementScreenshot = await myElement.screenshot({
type: 'png',
encoding: 'base64',
});
When I select and console.log the html for the element I see that everything is there that I want to capture with the screenshot. However my screenshot sometimes does not include a nested element.
If I visit the url I do see that the nested element loads in after the parent is visible. However even if I waitForSelector
on the nested element and then select the parent for screenshot it sometimes still does not include the nested element. I do not control the page content but it does appear to load in via a fetch
.
How can I be sure the nested element will be captured in the screenshot?