我正在使用Kameleo运行一个脚本,并且一直收到以下错误:
UnhandledPromiseRejectionWarning:未经处理的拒绝承诺。此错误起源于在异步函数中抛出而不带catch块,或者拒绝使用.catch()处理的承诺。在未处理的允诺拒绝时终止节点进程
这是我的密码:
const { KameleoLocalApiClient, BuilderForCreateProfile } = require('@kameleo/local-api-client');
const puppeteer = require("puppeteer");
(async () => {
const client = new KameleoLocalApiClient();
const baseProfiles = await client.searchBaseProfiles({
deviceType: 'desktop',
browserProduct: 'chrome'
});
// Create a new profile with recommended settings
// for browser fingerprinting protection
const requestBody = BuilderForCreateProfile
.forBaseProfile(baseProfiles[0].id)
.setRecommendedDefaults()
.build();
const profile = await client.createProfile({ body: requestBody });
// Start the browser
await client.startProfile(profile.id);
const browserWSEndpoint = `ws://localhost:5050/puppeteer/$(profile.id)`;
const browser = await puppeteer.connect({browserWSEndpoint, defaultViewport: null});
const page = await browser.newPage();
await page.goto("https://youtube.com");
})();发布于 2022-04-17 17:33:48
看来,您的await语句中有一条正在等待拒绝的承诺,并且您没有错误处理来捕获拒绝(因此也是UnhandledPromiseRejectionWarning)。您可以添加错误处理以捕获拒绝,然后记录错误是什么,这将导致您找到错误的原始来源:
const { KameleoLocalApiClient, BuilderForCreateProfile } = require('@kameleo/local-api-client');
const puppeteer = require("puppeteer");
(async () => {
try {
const client = new KameleoLocalApiClient();
const baseProfiles = await client.searchBaseProfiles({
deviceType: 'desktop',
browserProduct: 'chrome'
});
// Create a new profile with recommended settings
// for browser fingerprinting protection
const requestBody = BuilderForCreateProfile
.forBaseProfile(baseProfiles[0].id)
.setRecommendedDefaults()
.build();
const profile = await client.createProfile({ body: requestBody });
// Start the browser
await client.startProfile(profile.id);
const browserWSEndpoint = `ws://localhost:5050/puppeteer/$(profile.id)`;
const browser = await puppeteer.connect({browserWSEndpoint, defaultViewport: null});
const page = await browser.newPage();
await page.goto("https://youtube.com");
} catch(e) {
console.log(e);
// add appropriate logic to handle errors or cleanup here
}
})();而且,一旦您这样做,您可能会发现您的URL是错误的,因为您使用了错误的字符串模板语法。改变这一点:
const browserWSEndpoint = `ws://localhost:5050/puppeteer/$(profile.id)`;对此:
const browserWSEndpoint = `ws://localhost:5050/puppeteer/${profile.id}`;而不是。
发布于 2022-04-19 15:50:11
您正在以错误的方式设置browserWSEndpoint。而不是
const browserWSEndpoint = `ws://localhost:5050/puppeteer/$(profile.id)`;使用这个:
const browserWSEndpoint = `ws://localhost:5050/puppeteer/${profile.id}`;不同之处在于javascript字符串模板文本。在这里,您需要使用${}而不是$()
添加额外的尝试-捕捉块是一个很好的主意,建议是@jfriend00。我看到有一个404 HTTP状态代码。这是因为Kameleo找不到虚拟浏览器配置文件,因为profileId没有正确地添加到URL中。看起来是这样的:
ws://localhost:5050/puppeteer/$(profile.id)而不是这样:
ws://localhost:5050/puppeteer/df7dd197-5359-407d-8eb8-9d4f5ed13aab而且$(profile.id)不是一个有效的profileId
https://stackoverflow.com/questions/71903821
复制相似问题