【发布时间】:2018-06-07 00:44:24
【问题描述】:
在输入查询并单击按钮后,我试图从页面中提取特定元素。该页面不会导航到新的 URL:它只是返回我需要提取的新 HTML 内容。
这描述了我已经走了多远:
const puppeteer = require('puppeteer');
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};
const input_val = 'some query text';
(async() => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('http://target.com', { waitUntil: 'networkidle2' })
await page.waitFor('input[name=query]')
await page.evaluate((input_val) => {
document.querySelector('input[name=query]').value = input_val;
document.querySelector('.Button').click();
}, input_val)
// Now I want to console.log the <strong> tag fields
// innerText (will be 0-3 matching elements).
// The lines below describe in non-puppeteer what
// I need to do. But this has no effect.
const strongs = await page.$$('strong')
for(var i=0; i<strongs.length; i++) {
console.log(strongs[i].innerText);
}
await timeout(2000)
await page.screenshot({path: 'example.png'}) // this renders results page ok
browser.close();
})();
所以输入查询被正确输入,按钮点击被触发,屏幕截图显示网页已按预期响应。我只是不知道如何提取和报告相关位。
我一直试图了解整个 async/await 范式,但我对它还是很陌生。非常感谢您的帮助。
编辑 - Vaviloff 方法错误:
(node:67405) UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Cannot find context with specified id undefined
at Promise (/Users/user/node_modules/puppeteer/lib/Connection.js:200:56)
at new Promise (<anonymous>)
at CDPSession.send (/Users/user/node_modules/puppeteer/lib/Connection.js:199:12)
at ExecutionContext.evaluateHandle (/Users/user/node_modules/puppeteer/lib/ExecutionContext.js:79:75)
at ExecutionContext.evaluate (/Users/user/node_modules/puppeteer/lib/ExecutionContext.js:46:31)
at Frame.evaluate (/Users/user/node_modules/puppeteer/lib/FrameManager.js:326:20)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:160:7)
(node:67405) 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:67405) [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.
【问题讨论】:
-
请使用您当前的脚本更新问题,以便我们进行调试。可能是错字或遗漏了
await。另外:你的木偶版本是什么?一些旧版本有a bug producing this error。 -
我添加了一个用于测试该技术的工作脚本。
-
对我来说,将
await page.goto(url);更改为await page.goto(url, { waitUntil: 'networkidle2' });修复了错误。
标签: selenium-chromedriver puppeteer