Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

chromium - puppeteer doesn't open a url without protocol

const puppeteer = require('puppeteer');

const browser = await puppeteer.launch();
const page = await browser.newPage();

This one works

await page.goto('https://example.com');

This doesn't work (without the protocol i.e http/https)

await page.goto("www.example.com');

It throws error

Protocol error (Page.navigate): Cannot navigate to invalid URL

Why doesn't it append the protocol like it does when we open in Google Chrome?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Google Chrome Omnibox (Address Bar) has built in functionality to handle multiple complexities, such as: appending protocols, autocomplete, etc.

Puppeteer provides an API to control Chrome or Chromium over the DevTools Protocol, so much of this functionality is currently out of the scope of Puppeteer.

The Puppeteer documentation for the function page.goto() explicitly states:

The url should include scheme, e.g. https://.

This is because page.goto() utilizes Page.navigate from the Chrome DevTools Protocol.

The Chromium source code shows that navigation via Page.navigate is explicitly checked for validity, and if the URL is not valid, it will return the error, "Cannot navigate to invalid URL."

You can easily create a function in Node.js that will append protocols to URLs, and that could be a workaround for your issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...