UnhandledPromiseRejectionWarning:错误:协议错误(Page.navigate):当我传递有效 URL 时无法导航到无效 URL

Posted

技术标签:

【中文标题】UnhandledPromiseRejectionWarning:错误:协议错误(Page.navigate):当我传递有效 URL 时无法导航到无效 URL【英文标题】:UnhandledPromiseRejectionWarning: Error: Protocol error (Page.navigate): Cannot navigate to invalid URL when I am passing valid URLS 【发布时间】:2021-11-21 20:15:40 【问题描述】:

我正在尝试使用 puppeteer 在电子应用中抓取亚马逊商品的产品标题。 我正在使用的确切测试网址是 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 完全陌生,对于任何明显的错误,我深表歉意。

【问题讨论】:

console.log(link); 记录了什么? "amazon.ca/gp/product/…" 【参考方案1】:
const link = JSON.stringify(item);

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

const link = item;

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

const link = String(item);

甚至这个:

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

【讨论】:

以上是关于UnhandledPromiseRejectionWarning:错误:协议错误(Page.navigate):当我传递有效 URL 时无法导航到无效 URL的主要内容,如果未能解决你的问题,请参考以下文章

[Unhandled promise rejection: TypeError: null is not an object (evaluating '_reactNativeImageCropPic

等待 - 捕获错误 - UnhandledPromiseRejectionWarning

批量删除如何工作?

7月工作知识总计:

未处理的承诺拒绝 |重启命令

未处理的承诺拒绝警告(Cordova Angular)