I need to wait for an element to change classes.
The problem is when I use the waitForSelector
function, it wouldn't work because no new element is added to the DOM. However, the <div>
element changes its class name.
What's the right approach to wait for an element until its class name changes, or wait until a certain class name appears?
My current code:
import type { NextApiRequest, NextApiResponse } from "next";
const puppeteer = require("puppeteer");
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const browser = await puppeteer.launch({
executablePath:
"../../../../../../Program Files (x86)/Google/Chrome/Application/chrome.exe",
headless: false,
});
const page = await browser.newPage();
await page.goto("https://www.craiyon.com/", {
timeout: 0,
waitUntil: "domcontentloaded",
});
await page.waitForTimeout(1000);
await page.type(".svelte-1g6bo9g", "sausage");
await page.click("#generateButton");
const test = await page.waitForSelector(
".h-full.w-full.cursor-pointer.rounded-lg.border.border-medium-blue.object-cover.object-center.transition-all.duration-200.hover:scale-[0.97].hover:border-2.hover:border-grey",
{
timeout: 0,
}
);
await browser.close();
console.log(test);
res.status(200).json({ test: "test" });
}
This is the class name that changes later on:
.h-full.w-full.cursor-pointer.rounded-lg.border.border-medium-blue.object-cover.object-center.transition-all.duration-200.hover:scale-[0.97].hover:border-2.hover:border-grey
And finally this is the class name I'm trying to get: .grid.grid-cols-3.gap-1.sm:gap-2
.