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

Lea*_*one 19 python windows command-line

我试图以这种方式使用python在新标签中启动网站网址,但它在这两方面都没有用:

方法1:

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

和方法2:

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

如果我不添加参数(-new-tab http://www.google.com/),则可以打开默认页面.

Nad*_*mli 47

您需要使用该webbrowser模块

import webbrowser
webbrowser.open('http://www.google.com')
Run Code Online (Sandbox Code Playgroud)

[ 编辑 ]

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

webbrowser.get('firefox').open_new_tab('http://www.google.com')
Run Code Online (Sandbox Code Playgroud)

  • 如果用户的默认浏览器不是Firefox,您是否应该强制使用它? (3认同)

sth*_*sth 11

如果要使用参数启动程序,则子进程模块更适合:

import subprocess
subprocess.call([r'C:\Program Files\Mozilla Firefox\Firefox.exe',
    '-new-tab', 'http://www.google.com/'])
Run Code Online (Sandbox Code Playgroud)

  • 如果没有安装firefox怎么办?如果它安装在不同的目录中怎么办?这不是正确的方法.即使它在某些情况下有效 (4认同)