使用Python在Firefox(win)选项卡上启动网页

使用Python在Firefox(win)选项卡上启动网页

问题描述:

我尝试使用python以这种方式在新标签页中启动网站网址,但是没有以这两种方式工作:

I'm trying to launch a website url in a new tab using python in that way, but it didn't worked in these both ways:

方法1 :

os.system('C:\Program Files\Mozilla Firefox\Firefox.exe -new-tab http://www.google.com/');

和方法2:

os.startfile('C:\Program Files\Mozilla Firefox\Firefox.exe -new-tab http://www.google.com/');



如果不添加参数(-new-tab http://www.google.com/ ),它会正常工作,打开默认页面。

If I don't add the parameters (-new-tab http://www.google.com/) it works, opening the default page.

您需要使用 webbrowser 模块

You need to use the webbrowser module

import webbrowser
webbrowser.open('http://www.google.com')

[编辑]

如果您要在非默认浏览器中打开网址,请尝试:

If you want to open a url in a non-default browser try:

webbrowser.get('firefox').open_new_tab('http://www.google.com')