最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - No node found for selector in headless true mode of puppeteer - Stack Overflow

programmeradmin3浏览0评论

My enviroment:

  • Puppeteer version: 1.20.0
  • Platform / OS version: Ubuntu 18.04.3 LTS
  • Node.js version: 8.10.0
  • Chrome/78.0.3882.0

This error content was printed in terminal:

(node:18157) UnhandledPromiseRejectionWarning: Error: No node found for selector: #identifierNext
    at assert (/home/hoangdd3/node_modules/puppeteer/lib/helper.js:279:11)
    at DOMWorld.click (/home/hoangdd3/node_modules/puppeteer/lib/DOMWorld.js:366:5)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
  -- ASYNC --
    at Frame.<anonymous> (/home/hoangdd3/node_modules/puppeteer/lib/helper.js:111:15)
    at Page.click (/home/hoangdd3/node_modules/puppeteer/lib/Page.js:1037:29)
    at puppeteer.launch.then (/home/hoangdd3/pupperteer/example.js:15:16)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:18157) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18157) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

This my code:

const puppeteer = require('puppeteer');

puppeteer.launch({
    headless: true
}).then(async browser => {
    const page = await browser.newPage();
    await page.setViewport({width: 1920, height: 1080});
    await page.goto(';flowEntry=ServiceLogin',  {"waitUntil" : "networkidle0"});

    await page.waitFor(2000);

    await page.click('input[type=email]');
    await page.keyboard.sendCharacter('[email protected]');
    await page.click('#identifierNext');

    await page.waitFor(2000);

    await page.evaluate(() => document.querySelector('#password > div > div > div > input').click());
    await page.keyboard.sendCharacter('test');
    await page.evaluate(() => document.querySelector('#passwordNext').click());

    await page.screenshot({path: 'example.png'});

    await browser.close();
});

My enviroment:

  • Puppeteer version: 1.20.0
  • Platform / OS version: Ubuntu 18.04.3 LTS
  • Node.js version: 8.10.0
  • Chrome/78.0.3882.0

This error content was printed in terminal:

(node:18157) UnhandledPromiseRejectionWarning: Error: No node found for selector: #identifierNext
    at assert (/home/hoangdd3/node_modules/puppeteer/lib/helper.js:279:11)
    at DOMWorld.click (/home/hoangdd3/node_modules/puppeteer/lib/DOMWorld.js:366:5)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
  -- ASYNC --
    at Frame.<anonymous> (/home/hoangdd3/node_modules/puppeteer/lib/helper.js:111:15)
    at Page.click (/home/hoangdd3/node_modules/puppeteer/lib/Page.js:1037:29)
    at puppeteer.launch.then (/home/hoangdd3/pupperteer/example.js:15:16)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:18157) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18157) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

This my code:

const puppeteer = require('puppeteer');

puppeteer.launch({
    headless: true
}).then(async browser => {
    const page = await browser.newPage();
    await page.setViewport({width: 1920, height: 1080});
    await page.goto('https://accounts.google./signin/v2/identifier?flowName=GlifWebSignIn&flowEntry=ServiceLogin',  {"waitUntil" : "networkidle0"});

    await page.waitFor(2000);

    await page.click('input[type=email]');
    await page.keyboard.sendCharacter('[email protected]');
    await page.click('#identifierNext');

    await page.waitFor(2000);

    await page.evaluate(() => document.querySelector('#password > div > div > div > input').click());
    await page.keyboard.sendCharacter('test');
    await page.evaluate(() => document.querySelector('#passwordNext').click());

    await page.screenshot({path: 'example.png'});

    await browser.close();
});
Share Improve this question asked Sep 26, 2019 at 10:53 HoangHoang 86711 silver badges22 bronze badges 2
  • I have the same issue as you. Please advise if you've solved it. I believe there must be some sort of obfuscation to prevent clicking this button. – user2481095 Commented Nov 6, 2019 at 23:41
  • 1 @user2481095: please refer below answer of domlas. i do same that and success. – Hoang Commented Nov 7, 2019 at 8:52
Add a ment  | 

2 Answers 2

Reset to default 5

the problem you are facing refers to selectors, they simply do not exist :). html served in headless: true is different to the one on headless: false

Your snippet works correctly when used on headless: false

There is code which will work in headless: true

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({
        headless: true,
    });
    const page = await browser.newPage();

    await page.goto('https://accounts.google./signin/v2/identifier?flowName=GlifWebSignIn&flowEntry=ServiceLogin',  {"waitUntil" : "networkidle0"});
    await page.waitFor(2000);

    await page.click('#Email');
    await page.keyboard.sendCharacter('[email protected]');

    await page.click('#next');

    await page.waitFor(2000);

    await page.click('#Passwd');

    await page.keyboard.sendCharacter('test');
    await page.click('#signIn');

    await page.screenshot({path: 'example.png'});

    await browser.close();
})();

On screen example.png you can see info with incorrect password (test).

Looks like in await page.click('#identifierNext'); theare no element with id=identifierNext

insted of await page.click('#identifierNext');

try to use

const next = await page.waitForSelector('#identifierNext');
await next.click();

Different betwen click('#identifierNext') waitForSelector and click

waitForSelector - can wait for an element for 30,000s to add an element to DOM

click - if there no element in DOM right now, click will reject Promise

发布评论

评论列表(0)

  1. 暂无评论