我正在尝试创建一个应用程序,对随机单词进行谷歌图像搜索并选择/单击第一个结果图像。
我成功了,直到代码尝试选择结果图像并在我的终端中抛出以下错误:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection
id: 1): Error: Protocol error (Runtime.callFunctionOn): Cannot find
context with specified id undefined
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
const pup = require('puppeteer');
const random = require('random-words');
const url = 'http://images.google.com';
(async() => {
const browser = await pup.launch({headless: false});
const page = await browser.newPage();
await page.goto(url);
const searchBar = await page.$('#lst-ib');
await searchBar.click();
await page.keyboard.type(`${random()}`);
const submit = await page.$('#mKlEF');
await submit.click();
await page.keyboard.type(random());
await page.keyboard.press('Enter');
const pic = await page.evaluate(() => {
return document.querySelectorAll('img');
});
pic.click(); …Run Code Online (Sandbox Code Playgroud)