【问题标题】:for loop second time click on Dom element not working in puppeteerfor 循环第二次单击 Dom 元素在 puppeteer 中不起作用
【发布时间】:2018-12-12 06:49:46
【问题描述】:

我有主类别>子类别>项目来使用puppeteer选择随机项目我正在使用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});
        }
    }

【问题讨论】:

  • 可能那里没有元素。你用普通的 Chrome DevTools 控制台检查过代码吗?
  • 所有元素都在那里。
  • 请将 HTML 也添加到问题中。

标签: javascript testing puppeteer testcase


【解决方案1】:
 (Math.floor(Math.random() * 0) + 1) === 1 //true

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多