简体   繁体   English

在(Puppeteer 错误)中找不到选择器的节点

[英]No node found for selector in (Puppeteer Error)

This is the code I have given:这是我给出的代码:

const puppeteer = require('puppeteer');
(async () => {
    const baseUrl = 'example.com';
    const browser = await puppeteer.launch({ headless: true});
    const context = await browser.createIncognitoBrowserContext();
    const page = await context.newPage();

    function delay(time) {
        return new Promise(function (resolve) {
            setTimeout(resolve, time)
        });
    }

    await page.goto(baseUrl);

    await page.waitForSelector('[name="loginfmt"]');
    await page.type('[name="loginfmt"]', 'abc@amazon.com');
    await page.click('[type="submit"]');
    delay(1000);
    await page.authenticate({ username: `abc@amazon.com`, password: `abc@123` });

    await page.click((`[data-row-id="111-222-333-444"]`));
    await delay(1000);

    await page.pdf(
        {
            path: "Z1.pdf",
            printBackground: true,
            width: '1300px',
            height: '14200px'
        }
    )

    browser.close();
})()

When inspected the page with document.querySelectorAll( [data-row-id="111-222-333-444"] );当使用 document.querySelectorAll( [data-row-id="111-222-333-444"] ); 检查页面时it returned a NodeList array which consisted the details which I have been looking for.它返回了一个NodeList数组,其中包含我一直在寻找的详细信息。 However when tried the same via Puppeteer it terminates with an error saying "Error: No node found for selector: [data-row-id="111-222-333-444"]"但是,当通过 Puppeteer 尝试相同的操作时,它会以错误提示“错误:找不到选择器的节点:[data-row-id="111-222-333-444"]”而终止

Tried using: "const button= await page.$(( [data-row-id="111-222-333-444"] ); button.click();"尝试使用:“const button= await page.$(( [data-row-id="111-222-333-444"] ); button.click();”

I have been trying to fix this since a long time.很长一段时间以来,我一直在尝试解决此问题。 It would be really helpful if I get a fix for this.如果我能解决这个问题,那将非常有帮助。

Try using this code:尝试使用此代码:

    const puppeteer = require('puppeteer');
(async () => {
    const baseUrl = 'example.com';
    const browser = await puppeteer.launch({ headless: true});
    const context = await browser.createIncognitoBrowserContext();
    const page = await context.newPage();
 

    await page.goto(baseUrl);

    await page.waitForSelector('[name="loginfmt"]');
    await page.type('[name="loginfmt"]', 'abc@amazon.com');
    await page.click('[type="submit"]');
    await page.waitForTimeout(1000) 
    await page.authenticate({ username: `abc@amazon.com`, password: `abc@123` });

    await page.waitForTimeout(5000) 

    await page.$(`[data-row-id="111-222-333-444"]`);

    await page.click((`[data-row-id="111-222-333-444"]`));
    await page.waitForTimeout(1000) 

    await page.pdf(
        {
            path: "Z1.pdf",
            printBackground: true,
            width: '1300px',
            height: '14200px'
        }
    )

    browser.close();
})()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM