首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnhandledPromiseRejectionWarning在使用Kameleo和Puppeteer时

UnhandledPromiseRejectionWarning在使用Kameleo和Puppeteer时
EN

Stack Overflow用户
提问于 2022-04-17 17:03:51
回答 2查看 97关注 0票数 0

我正在使用Kameleo运行一个脚本,并且一直收到以下错误:

UnhandledPromiseRejectionWarning:未经处理的拒绝承诺。此错误起源于在异步函数中抛出而不带catch块,或者拒绝使用.catch()处理的承诺。在未处理的允诺拒绝时终止节点进程

这是我的密码:

代码语言:javascript
复制
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");
})();
EN

回答 2

Stack Overflow用户

发布于 2022-04-17 17:33:48

看来,您的await语句中有一条正在等待拒绝的承诺,并且您没有错误处理来捕获拒绝(因此也是UnhandledPromiseRejectionWarning)。您可以添加错误处理以捕获拒绝,然后记录错误是什么,这将导致您找到错误的原始来源:

代码语言:javascript
复制
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是错误的,因为您使用了错误的字符串模板语法。改变这一点:

代码语言:javascript
复制
const browserWSEndpoint = `ws://localhost:5050/puppeteer/$(profile.id)`;

对此:

代码语言:javascript
复制
const browserWSEndpoint = `ws://localhost:5050/puppeteer/${profile.id}`;

而不是。

票数 0
EN

Stack Overflow用户

发布于 2022-04-19 15:50:11

您正在以错误的方式设置browserWSEndpoint。而不是

代码语言:javascript
复制
const browserWSEndpoint = `ws://localhost:5050/puppeteer/$(profile.id)`;

使用这个:

代码语言:javascript
复制
const browserWSEndpoint = `ws://localhost:5050/puppeteer/${profile.id}`;

不同之处在于javascript字符串模板文本。在这里,您需要使用${}而不是$()

添加额外的尝试-捕捉块是一个很好的主意,建议是@jfriend00。我看到有一个404 HTTP状态代码。这是因为Kameleo找不到虚拟浏览器配置文件,因为profileId没有正确地添加到URL中。看起来是这样的:

代码语言:javascript
复制
ws://localhost:5050/puppeteer/$(profile.id)

而不是这样:

代码语言:javascript
复制
ws://localhost:5050/puppeteer/df7dd197-5359-407d-8eb8-9d4f5ed13aab

而且$(profile.id)不是一个有效的profileId

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71903821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档