首页 > 解决方案 > 如何在 Ubuntu 中使用 Python 在 firfox 中打开网页

问题描述

我正在做一个项目,我需要url使用 python 在 Ubuntu 中打开一个 Firefox。为此,我有以下代码:

import webbrowser

webbrowser.open('https://www.google.com')

这在 Windows 中运行良好,并且google.com在操作系统中可用的默认浏览器中打开。但在 Ubuntu 的情况下,我遇到了 Firefox 的以下错误:

[6858:6858:0929/111955.830869:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
[6868:6868:0929/111955.853440:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
Running Firefox as root in a regular user's session is not supported.

我想安装 chrome,然后在 webbrowser 代码中使用 chrome。但还是一样。我什至尝试修改google-chrome文件,但它一直抛出同样的错误。出于某种原因,我必须将代码作为 sudo 运行,因此需要它。任何人都可以请帮忙。谢谢

标签: pythonfirefoxsudopython-webbrowser

解决方案


我会推荐Selenium,但你需要先安装 webdriver

from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://www.google.com')

推荐阅读