Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 608
Abdullah Avatar asked Aug 30 '18 06:08

Abdullah


People also ask

Can puppeteer use Chrome instead of Chromium?

(However, it is possible to force Puppeteer to use a separately-installed version Chrome instead of Chromium via the executablePath option to puppeteer. launch .

What is headless mode in puppeteer?

Puppeteer is a Node.js library which provides a high-level API to control Chrome/Chromium over the DevTools Protocol. Puppeteer runs in headless mode by default, but can be configured to run in full (non-headless) Chrome/Chromium.

Does puppeteer work with Chrome?

Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium.

Can puppeteer run in browser?

Puppeteer lets you automate the testing of your web applications. With it, you can run tests in the browser and then see the results in real-time on your terminal. Puppeteer uses the WebDriver protocol to connect with the browser and simulate user interaction with HTML elements or pages.


1 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.

like image 116
Grant Miller Avatar answered Jan 04 '23 02:01

Grant Miller