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

javascript - cannot find context with specified id undefined - Stack Overflow

programmeradmin1浏览0评论

I am trying to create an app that does a google image search of a random word and selects/clicks the first result image.

I am successful until the code is attempting to select the result image and it throws the following error in my terminal:

 UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection 
 id: 1): Error: Protocol error (Runtime.callFunctionOn): Cannot find 
 context with specified id undefined

Here is my code:

const pup = require('puppeteer');
const random = require('random-words');
const url = '';

(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();
})();

I am trying to create an app that does a google image search of a random word and selects/clicks the first result image.

I am successful until the code is attempting to select the result image and it throws the following error in my terminal:

 UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection 
 id: 1): Error: Protocol error (Runtime.callFunctionOn): Cannot find 
 context with specified id undefined

Here is my code:

const pup = require('puppeteer');
const random = require('random-words');
const url = 'http://images.google.';

(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();
})();
Share Improve this question edited Jun 6, 2022 at 20:40 ggorlen 58k8 gold badges114 silver badges157 bronze badges asked May 23, 2018 at 5:19 Aaron LeeAaron Lee 611 silver badge3 bronze badges 1
  • See When would an error "Cannot find context with specified id undefined" happen? #1325. – ggorlen Commented Jun 6, 2022 at 20:47
Add a ment  | 

2 Answers 2

Reset to default 1

document.querySelectorAll('img') is not serialisable, so it returns undefined (see this issue as reference)

Please use something like: (depends on which element you want to click)

await page.$$eval('img', elements => elements[0].click());

This is a long dead thread but if anyone runs into this issue and the above answer does not apply to you, try adding a simple await page.waitForTimeout(2000). My test was pleting but I was getting this error when attempting to await browser.close(); Adding the wait after searching for my final selector seems to have resolved the issue.

发布评论

评论列表(0)

  1. 暂无评论