首页 > 解决方案 > UnhandledPromiseRejectionWarning:错误:协议错误(Page.navigate):当我传递有效 URL 时无法导航到无效 URL

问题描述

我正在尝试使用 puppeteer 在电子应用程序中抓取亚马逊商品的产品标题。The exact test url I am using is https://www.amazon.ca/gp/product/B088CK6GP1?pf_rd_r=X4CXHQ3R45BFH3T0VF4X&pf_rd_p=05326fd5-c43e-4948-99b1-a65b129fdd73&pd_rd_r=8146d8bb-8a19-46d1-a3c8-936ace2ee64e&pd_rd_w=63lRt&pd_rd_wg= OOZ2e&ref_=pd_gw_unk

当我在单独的测试文件中而不是在电子应用程序中运行它并且我找不到修复错误的方法时,它可以工作

这是刮板文件

const puppeteer = require('puppeteer')

getname = async function(url){
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);
    await page.waitForSelector('#productTitle')
    const name = await page.evaluate(() => {
       return document.getElementById("productTitle").innerText;
    })
    await browser.close();
    return name;
}



module.exports = { getname };

测试文件

const scraper = require('./scrape.js');

const url = "https://www.amazon.ca/gp/product/B088CK6GP1?pf_rd_r=F20ADBW6SQS0H1MCSSMC&pf_rd_p=05326fd5-c43e-4948-99b1-a65b129fdd73&pd_rd_r=d92fb039-6465-47e1-bda2-6d75b9f5104e&pd_rd_w=UeD0T&pd_rd_wg=SoFi6&ref_=pd_gw_unk";
const name = scraper.getname(url);
name.then(function(resualt){
    console.log(typeof resualt);
    console.log(resualt);
})

电子密码。使用刮板的部分在 //catch item:add 下

const electron = require('electron');
const scraper = require('./scrape.js');
const url = require('url');
const path = require('path');
const { protocol } = require('electron');
const { stringify } = require('querystring');

const {app, BrowserWindow, Menu, ipcMain} = electron;

let mainWindow;
let addWindow;

process.env.NODE_ENV = 'production';

// Listen for the app to be ready


app.on('ready', function(){
    mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
        }
    });
    //load html
    mainWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'mainwindow.html'),
        protocol: 'file',
        slashes: true
    }));
    //Quit app when closed
    mainWindow.on('closed', function(){
        app.quit();
    })
    //build main menu
    const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
    //insert menu
    Menu.setApplicationMenu(mainMenu)

});

//Handel add window

function createAddWindow(){
    addWindow = new BrowserWindow({
        width: 300,
        height: 200,
        title: 'Add Item',
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
        }
    });

    //load html
    addWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'addwindow.html'),
        protocol: 'file',
        slashes: true
    }));

    //Garbage collection
    addWindow.on('close', function(){
        addWindow = null;
    });
}

function createLogWindow(){
    addWindow = new BrowserWindow({
        width: 300,
        height: 200,
        title: 'Add Item',
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
        }
    });

    //load html
    addWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'login.html'),
        protocol: 'file',
        slashes: true
    }));

    //Garbage collection
    addWindow.on('close', function(){
        addWindow = null;
    });
}


//catch item:add

ipcMain.on('item:add', function(e, item){
    const link = JSON.stringify(item);
    console.log(link);
    const name = scraper.getname(link);
    name.then(function(resualt){
        console.log(resualt);
        mainWindow.webContents.send('item:add', resualt);
    })
    addWindow.close();
});

ipcMain.on('details', function(e, details){
    console.log(details["email"]);
    console.log(details["password"]);
    addWindow.close();
});



//create menu template

const mainMenuTemplate = [
    {
        label: 'Flie',
        submenu:[
            {
                label: 'Add Item',
                click(){
                    createAddWindow();
                }
            },
            {
                label: 'Clear Item',
                click(){
                    mainWindow.webContents.send('item:clear');
                }
            },
            {
                label: 'Run Check',
                accelerator: process.platform == 'darwin' ? 'Comamand+R' :
                'Ctrl+R',
                click(){
                    createLogWindow();
                }
            },
            {
                label: 'Quit',
                accelerator: process.platform == 'darwin' ? 'Comamand+Q' :
                'Ctrl+Q',
                click(){
                    app.quit();
                }
            }
        ]
    }
];

//If mac add empty object to menu
if(process.platform == 'darwin'){
    mainMenuTemplate.unshift({});
}

// add dev tool if not in production

if(process.env.NODE_ENV === 'production'){
    mainMenuTemplate.push({
        label: "Developer Tools",
        submenu:[
            {
                label: 'Toggle Dev Tools',
                accelerator: process.platform == 'darwin' ? 'Comamand+I' :
                'Ctrl+I',
                click(item, focusedWindow){
                    focusedWindow.toggleDevTools();
                }
            },
            {
                role: 'reload'
            }
        ]
    });
}

我对 JS、选举者和 puppeteer 完全陌生,对于任何明显的错误,我深表歉意。

标签: javascripthtmlelectronpuppeteer

解决方案


const link = JSON.stringify(item);

看来您已经在item. JSON.stringify(item)返回用引号括起来的相同字符串。这些引号使 URL 字符串无效。试试这个:

const link = item;

或者,如果 item 是一个带有一些返回 URL 的方法的对象toJSON()toString()试试这个:

const link = String(item);

甚至这样:

const link = JSON.stringify(item).slice(1, -1);

推荐阅读