javascript - UnhandledPromiseRejectionWarning:错误:协议错误(Page.navigate):无法导航到无效 URL
问题描述
我正在尝试使用经过身份验证的工作流从文本文件中使用 lighthouse 扫描一组 url。工作流在进行身份验证之前工作正常,当它尝试从 url.txt 中选择 url 信息时,我收到以下错误
loading home url
(node:9146) UnhandledPromiseRejectionWarning: Error: Protocol error (Page.navigate): Cannot navigate to invalid URL
at /Users/jagadeeshshankarbalasubramaniam/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:208:63
at new Promise (<anonymous>)
at CDPSession.send (/Users/jagadeeshshankarbalasubramaniam/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Connection.js:207:16)
at navigate (/Users/jagadeeshshankarbalasubramaniam/node_modules/puppeteer-core/lib/cjs/puppeteer/common/FrameManager.js:108:47)
Urls.txt 文件包含格式为 https://xx/xx/xx 的 url。
即使我提到puppeteer 也不会打开没有协议的 url请求使用 await page.goto 我已经使用如下
console.log('loading home url');
await page.goto(testurl, {
waitUntil: 'networkidle0'});
实际代码
const fs = require('fs');
const util = require('util');
const puppeteer = require('puppeteer-core');
const lighthouse = require('lighthouse');
const totp = require('totp-generator');
const lines = require('lines');
const URL = require('url').Url;
const config = require('lighthouse/lighthouse-core/config/lr-desktop-config.js');
const reportGenerator = require('lighthouse/lighthouse-core/report/report-generator');
const now = new Date().toISOString()
const readFile = util.promisify(fs.readFile);
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
}
function token() {
return totp(process.env.OTP);
}
async function getUrlsFromFile() {
const contents = await readFile('urls.txt');
return lines.toString().split('\n')
}
async function nav_to_site() {
testurls = await getUrlsFromFile()
const url = 'https://xxx/landing';
const profile_url = 'https://xxx/account/profile';
for (let testurl of testurls) {
const browser = await puppeteer.launch({
headless: true,
defaultViewport: null,
executablePath: '/usr/local/bin/chromium',
args: ['--headless', '--no-sandbox', '--remote-debugging-port=9222', '--disable-gpu'],
});
console.log('Navigating to site.com');
await delay(10000);
const page = (await browser.pages())[0];
await page.goto(url, {
waitUntil: 'networkidle0'
});
// adding delay of 10 sec
await delay(10000);
await page.setDefaultNavigationTimeout(0);
console.log('enter username');
// probably some issue loading this page.. decrease timeout to make it faster
try {
await page.type('#email', process.env.EMAIL)
} catch (err) {
console.log(err);
}
await page.click('#next-button');
// adding delay of 10 secs
await delay(10000);
await page.click('#credsDiv')
// adding delay of 10 sec
await delay(10000);
console.log('w3 email');
try {
await page.type('[type="email"]', process.env.EMAIL);
} catch (err) {
console.log(err);
}
console.log('hit tab');
await page.keyboard.press('Tab', {
delay: 100
});
console.log('password');
try {
await page.type('#password-input', process.env.PASSWORD);
} catch (err) {
console.log(err);
}
console.log('click signin')
await page.click('#login-button');
// adding delay of 10 sec
await delay(10000);
// OTP is not prompted sometimes?
try {
console.log('check for otp')
otp_token = token();
await page.type('#otp-input', otp_token);
await page.keyboard.press('Enter')
} catch (err) {
console.log('otp not prompted')
}
// adding delay of 10 sec
await delay(10000);
await page.goto(profile_url, {
waitUntil: 'networkidle0'
});
try {
await page.waitForFunction(
'document.querySelector("body").innerText.includes("'.concat(process.env.EMAIL).concat('")')
);
console.log('login verified');
} catch (err) {
console.log(err);
console.log('login failed');
process.exit(1);
}
console.log('loading home url');
await page.goto(testurl, {
waitUntil: 'networkidle0'
});
console.log('Running lighthouse on', testurl);
page = (await browser.pages())[0];
const report = await lighthouse(page.url(), {
port: 9222,
output: 'json',
logLevel: 'info',
disableDeviceEmulation: true,
budgetPath: 'budget.json',
chromeFlags: ['--disable-gpu', '--no-sandbox', '--disable-storage-reset']
}, config);
const json = reportGenerator.generateReport(report.lhr, 'json');
const html = reportGenerator.generateReport(report.lhr, 'html');
console.log(`Lighthouse scores: ${report.lhr.score}`);
console.log('Writing results...');
fs.writeFileSync('/xxx/lighthouse'.concat('_', now).concat('.json'), json);
fs.writeFileSync('/xxx/lighthouse'.concat('_', now).concat('.html'), html);
console.log('Done!');
await page.close();
await browser.close();
}
}
nav_to_site()
解决方案
推荐阅读
- c++ - 为什么内存清理程序会报告 std::map 使用了未初始化的值?
- architecture - 服务启动时对其他服务和基础设施的依赖
- javascript - 甚至无法在浏览器中运行代码,因为 localhost 拒绝连接
- amazon-web-services - 在移动设备或 Chrome 隐身模式下,静态 s3 站点会出现 403 错误
- amazon-web-services - AWS Appflow <-> Salesforce 集成
- sql - Oracle SQL在单元格中查找最大位数
- android - Android:从 Firebase 下载的 dex 不起作用:“没有找到 dex 位置的原始 dex 文件”
- javascript - 无法提交表单操作,因为它被覆盖隐藏 - 我尝试过 Z-Index 和 Pointer-events:none; CSS/反应
- javascript - 如果 li id 为空,则隐藏 div
- angular - fullPage.js 不会在组件更改时破坏