PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, December 2, 2022

[FIXED] How to fix , Bug: Cannot read property 'contentFrame' of null using puppeteer

 December 02, 2022     iframe, puppeteer, typescript     No comments   

Issue

I am experiencing a bug when I request an api that I implement. Once I have used one of the endpoints, it shows me the error following error, which can be seen below.

There will be no way to control that error?

Error message:

TypeError: Cannot read property 'contentFrame' of null
    at Object.getAnimeVideo (C:\Users\c\Desktop\ryuanime\src\api\scraper.ts:89:37)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Code:

const getAnimeVideo = async (id: string, chapter: number) => {
  const BASE_URL = `${url}${id}/${chapter}/`;
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(BASE_URL);
  const elementHandle = await page.$('.player_conte');
  const frame = await elementHandle.contentFrame(); //line 89 error
  const video = await frame.$eval('#jkvideo_html5_api', el =>
    Array.from(el.getElementsByTagName('source')).map(e => e.getAttribute("src")));
  await browser.close();
  return video;
}

Solution

Problem

elementHandle is null. That's why you get the error that there is not function contentFrame on null. page.$ is null, when the selector cannot be found.

Solution

To solve the problem, you can do two things:

  • Is the selector actually the selector you are looking for? Is it maybe inside a frame and not on the page itself or maybe misspelled?
  • Maybe the element is not present at the time when your script is running? Maybe the page asynchronously loads more data and change the DOM? In that case, you can use page.waitForSelector like this to wait until the element is present:

    const elementHandle = await page.waitForSelector('.player_conte');
    


Answered By - Thomas Dondorf
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing