Typescript 如何修复错误:无法读取属性';contentFrame';使用木偶演员的空值
当我请求我实现的api时,我遇到了一个bug。一旦我使用了其中一个端点,它就会显示错误后的错误,如下所示 没有办法控制那个错误 错误消息:Typescript 如何修复错误:无法读取属性';contentFrame';使用木偶演员的空值,typescript,iframe,puppeteer,findbugs,Typescript,Iframe,Puppeteer,Findbugs,当我请求我实现的api时,我遇到了一个bug。一旦我使用了其中一个端点,它就会显示错误后的错误,如下所示 没有办法控制那个错误 错误消息: TypeError:无法读取null的属性“contentFrame” 在Object.getAnimeVideo(C:\Users\C\Desktop\ryuanime\src\api\scraper.ts:89:37) 在进程中。_tick回调(内部/process/next_tick.js:68:7) 代码: const getAnimeVideo=
TypeError:无法读取null的属性“contentFrame”
在Object.getAnimeVideo(C:\Users\C\Desktop\ryuanime\src\api\scraper.ts:89:37)
在进程中。_tick回调(内部/process/next_tick.js:68:7)
代码:
const getAnimeVideo=async(id:string,chapter:number)=>{
const BASE_URL=`${URL}${id}/${chapter}/`;
const browser=wait puppeter.launch();
const page=wait browser.newPage();
等待页面。转到(基本URL);
const elementHandle=等待页面。$('.player_conte');
const frame=await elementHandle.contentFrame();//第89行错误
const video=等待帧。$eval('#jkvideo_html5_api',el=>
from(el.getElementsByTagName('source')).map(e=>e.getAttribute(“src”));
等待浏览器关闭();
返回视频;
}
问题
elementHandle
为null
。这就是为什么在null
上没有函数contentFrame
。当找不到选择器时,为null
解决方案
要解决此问题,您可以做两件事:
- 选择器实际上就是您要查找的选择器吗?它可能在一个框架内,而不是在页面上,或者可能拼写错误
- 可能在脚本运行时元素不存在?也许页面会异步加载更多数据并更改DOM?在这种情况下,您可以这样使用以等待元素出现:
const elementHandle=wait page.waitForSelector('.player_conte');