我的环境:
此错误内容已在终端上打印:
(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.
这是我的代码:
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.com/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();
});
看起来像await page.click('#identifierNext');
中没有id = identifierNext的元素
插入await page.click('#identifierNext');
尝试使用
const next = await page.waitForSelector('#identifierNext');
await next.click();
[之间不同click('#identifierNext')
waitForSelector and click
waitForSelector-可以等待30,000s的元素将元素添加到DOM
单击-如果DOM中现在没有元素,则单击将拒绝Promise
您面临的问题涉及选择器,它们根本不存在:)。 headless: true
中投放的html与headless: false
]中投放的html不同
您的代码段在headless: false
上使用时可以正常工作
[有些代码可在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.com/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(); })();
在屏幕
example.png
上,您会看到密码不正确(测试)的信息。