Javascript 未处理的PromisejectionWarning:错误:协议错误(Page.navigate):无法导航到无效URL
我正在尝试使用lighthouse从文本文件中使用经过身份验证的工作流扫描url集。工作流在经过身份验证之前工作正常,当它尝试从url.txt中选择url信息时,我得到以下错误Javascript 未处理的PromisejectionWarning:错误:协议错误(Page.navigate):无法导航到无效URL,javascript,node.js,puppeteer,lighthouse,Javascript,Node.js,Puppeteer,Lighthouse,我正在尝试使用lighthouse从文本文件中使用经过身份验证的工作流扫描url集。工作流在经过身份验证之前工作正常,当它尝试从url.txt中选择url信息时,我得到以下错误 loading home url (node:9146) UnhandledPromiseRejectionWarning: Error: Protocol error (Page.navigate): Cannot navigate to invalid URL at /Users/jagadeeshshanka
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)
实际代码
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()
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()