没有协议,伪造者不会打开网址 [英] puppeteer doesn't open a url without protocol

查看:98
本文介绍了没有协议,伪造者不会打开网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const puppeteer = require('puppeteer');

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

这一项有效

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

这不起作用(没有协议,即http / https)

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

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

抛出错误


协议错误(Page.navigate):无法导航至无效的URL

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

为什么它不像我们在Google Chrome中打开时那样附加协议?

推荐答案

Google Chrome浏览器多功能框(地址栏)具有内置的功能来处理多种复杂性,例如:附加协议,自动完成等。

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

Puppeteer提供了一个API,可通过 DevTools协议来控制Chrome或Chromium。该功能目前不在Puppeteer的范围内。

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.

函数 <$的Puppeteer文档c $ c> page.goto() 明确指出:

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


该网址应包含方案,例如 https://

这是因为 page.goto()使用 Page.navigate 来自Chrome DevTools协议。

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

铬源代码显示通过 Page.navigate 进行的导航已被明确检查有效性,并且如果URL无效,它将返回错误无法导航到无效URL。

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

您可以轻松地在Node.js中创建一个将协议附加到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.

这篇关于没有协议,伪造者不会打开网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆