gpt4 book ai didi

Javascript 无法读取 null 的属性 'innerText' - puppeteer

转载 作者:太空宇宙 更新时间:2023-11-04 01:20:51 25 4
gpt4 key购买 nike

使用此函数时有时会出现此错误:我的函数运行了 2/3 次,但我想纠正它。我正在使用 puppeteer 进行导航并检查我的代理是否正常工作。

无法将属性“innerText”设置为 null

请问如何解决...

这是我的代码。

let proxyValidity = waiting("Checking proxy Validity", 800);
try {
await LOG('Trying to validate IP using an API');
await page.goto(ipValidityUrl, { waitUntil: "load", timeout: 30000 });
} catch (err) {
await LOG('Error occured during loading IP validation API');
await page.close();
await closeBrowser(browser);
stopWaiting(proxyValidity, (stdClrs.FgRed + "ERROR"));

return {
errorId: 3,
msg: 'Unknown Proxy Error',
error: err
};
}
await LOG('IP validation URL loaded');

let proxyInfo = await page.evaluate(() => {
let div = document.querySelector('body > pre');
console.log(div);
jsonObject = JSON.parse(div.innerText);
key = Object.keys(jsonObject);
console.log(jsonObject[key]);
return jsonObject[key];
})
await LOG(`Proxy information recorded: ${proxyInfo}`);

await LOG('Checking for validity of IP');
let isValid = defaultData.proxyAllowedCountries.find((element) => {
return (proxyInfo[0] == element)
}) == proxyInfo[0];

错误代码:

UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'innerText' of null
at __puppeteer_evaluation_script__:4:35
[...]

最佳答案

您可以在调用评估函数之前添加一个waitForSelector

await page.waitForSelector('body > pre');
await page.evaluate(...);

关于Javascript 无法读取 null 的属性 'innerText' - puppeteer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59340100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com