After programming for a long time I am now trying my to get practical with Javascript. I want to load a page, and click a button and then check the available variables to test if they are looking good.
I was able to do this succesfully for just a normal page visit, however after simulating a button click that generates a popup I am not able to get the variable anymore.
Here are the steps that I want to take:
- Open the page, accept cookies
- Check the dataLayer
- Click a button to add a product in a basket (generates some kind of popup/something over the normal page)
- Check the dataLayer again
Step 2 works, but step 4 returns Undefined. I suspect it may be because it is focusing on the wrong thing, but I did not find a way to resolve this.
Here is the minimal code that reproduces the problem, I swapped out the link with example.com to be safe.
// Loading dependencies
const puppeteer = require('puppeteer');
// Opening the browser
(async () => {
const browser = await puppeteer.launch({
headless: false,
defaultViewport: false
})
// Simulating the user behavior
const page = await browser.newPage()
await page.goto('https://www.example.com/')
await page.click('button#onetrust-accept-btn-handler')
let dataLayer1 = await page.evaluate(() => {
return window.dataLayer
})
await page.click('button.button.button--solid.button--custom.button--color-primary.add-to-cart')
let dataLayer2 = await page.evaluate(() => {
return window.dataLayer
})
console.log('First output')
console.log(dataLayer1)
console.log('Second output')
console.log(dataLayer2)
console.log('Finished')
})();
I am not sure if it is relevant but I am running on windows and updated everything to the latest versions.