for 循环第二次单击 Dom 元素在 puppeteer 中不起作用

for loop second time click on Dom element not working in puppeteer

我有主类别 > 子类别 > 项目到 select 使用人偶操作的随机项目我正在使用 for 循环但是 for 循环的第二次迭代它不起作用...

它给出类似 enter image description here

的错误
 for (let i = 0; i < 3; i++) {
        this.menuCategoryLength = await page.$$eval(".menu-categories .slick-slide", div => div.length);
        this.categoryNo = Math.floor(Math.random() * this.menuCategoryLength) + 1;
        await page.click(".menu-categories .slick-slide:nth-child(" + this.categoryNo + ")", {delay: 200});

        await page.waitForSelector(".menu-subcategories");
        this.menuSubCategoryLength = await page.$$eval(".menu-subcategories .slick-slide", div => div.length);
        this.subCategoryNo = Math.floor(Math.random() * this.menuSubCategoryLength) + 1;
        await page.click(".menu-subcategories .slick-slide:nth-child(" + this.subCategoryNo + ")", {delay: 200});

        this.menuItem = await page.$(".menu-items .menu-item-wrap", {visible: true});

        if (this.menuItem) {
            this.menuItemLength = await page.$$eval(".menu-items .menu-item-wrap", div => div.length);
            this.max = this.menuItemLength > 1 && this.menuItemLength < 4 ? this.menuItemLength : 3;
            this.itemNo = this.menuItemLength === 1 ? 1 : Math.floor(Math.random() * this.max) + 1;
            await page.click(".menu-item-wrap:nth-child(" + this.itemNo + ")", {delay: 200});
        }
    }
 (Math.floor(Math.random() * 0) + 1) === 1 //true

可能 var this.menuCategoryLength 为 0,您的代码尝试获取 someSelector:nth-child(1)