puppeteer doesn't open a url without protocol

Viewed 11303
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?

2 Answers

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.

I got the same error when I was sending the url as an array of arrays

const urls = [["https://www.example1.com"], ["https://www.example2.com]]

destructuring it solved it for me

urls = [].concat(...urls)
for(let url of urls) {
    await page.goto(url)
 }
Related