首页 > 解决方案 > 如何从 Python 或 CLI 在 Firefox 中打开新标签页?

问题描述

我正在使用 CLI 方式为多个站点打开 Firefox。我正在使用以下代码,但它在我的 Mac 上的第一个站点卡住了:

def launch_sites(is_linux=True):
    if is_linux:
        cmd = 'firefox --new-tab http://adnansiddiqi.me'
        push = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        result = push.stdout.read()
        result = str(result, 'utf-8')

        cmd = 'firefox --new-tab http://google.com'
        push = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        result = push.stdout.read()
        result = str(result, 'utf-8')
    else:
        cmd = '/Applications/Firefox.app/Contents/MacOS/firefox-bin http://adnansiddiqi.me'
        push = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        result = push.stdout.read()
        result = str(result, 'utf-8')

        cmd = '/Applications/Firefox.app/Contents/MacOS/firefox-bin http://google.com'
        push = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        result = push.stdout.read()
        result = str(result, 'utf-8')

目标是一次在选项卡中打开所有这些站点。我得到的错误:

C[GFX1-]: Receive IPC close with reason=AbnormalShutdown
[GFX1-]: Receive IPC close with reason=AbnormalShutdown
Exiting due to channel error.
Exiting due to channel error.
[GFX1-]: Receive IPC close with reason=AbnormalShutdown

[GFX1-]: Receive IPC close with reason=AbnormalShutdown                         
Exiting due to channel error.
Exiting due to channel error.

标签: pythonautomation

解决方案


你可以试试这个

firefox --help

输出是

......
  --new-window <url> Open <url> in a new window.
  --new-tab <url>    Open <url> in a new tab.
......

推荐阅读