Typescript 如何修复错误:无法读取属性';contentFrame';使用木偶演员的空值

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=

当我请求我实现的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=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');
    

谢谢,成功了@Thomas Dondorf实现的函数返回视频的src。现在。。发生在我身上的是,它向我显示了前一个请求的src。也许是异步/等待时间的问题?@ChrisMichael这是一个与文章中提到的问题不同的问题。@ChrisMichael如果没有更多的信息,很难说是什么原因造成的。您可能想问另一个问题,并提供有关该页面的更多信息。如果对您有帮助,请随时联系:)