简体   繁体   English

如何使用Python启动带有多个选项卡的新Firefox窗口

[英]How to launch new Firefox window with multiple tabs using Python

I want to create a MSWindows Python program that would launch a new Firefox window with multiple tabs each time it is run. 我想创建一个MSWindows Python程序,每次运行时都会启动一个带有多个选项卡的新Firefox窗口。 For example if I want to search "hello", a new window pops out (even if a Firefox window is already open) and then launches a Google and Bing tabs searching for "hello". 例如,如果我想搜索“hello”,则会弹出一个新窗口(即使Firefox窗口已经打开),然后启动Google和Bing标签搜索“hello”。 If I change the keyword to "world", a new browser pops out again with Google and Bing tabs searching for "world". 如果我将关键字更改为“world”,则会再次弹出一个新的浏览器,Google和Bing标签会搜索“world”。

I've looked at the webbrowser module but couldn't get it to: 1. Launch a new browser when a browser is already open: eg webbrowser.open(' http://www.google.com ',new=1) will instead open a new tab 2. Launch multiple tabs simultaneously in the same window 我查看了webbrowser模块,但无法理解:1。当浏览器已经打开时启动一个新浏览器:例如webbrowser.open(' http : //www.google.com',new = 1)将改为打开一个新选项卡2.在同一窗口中同时启动多个选项卡

Appreciate the help. 感谢帮助。

Thanks. 谢谢。

webbrowser just doesn't give you this degree of control. webbrowser只是没有给你这种程度的控制。 Use subprocess instead, to explicitly launch firefox with a new window and then add tabs to it. 请改用subprocess ,使用新窗口显式启动firefox,然后为其添加制表符。 The firefox command line arguments reference is here , but, briefly, what you want is one firefox.exe -new-window <url> (using the URL you want in lieu of <url> of course), then one or more firefox.exe -new-tab <url> (ditto). firefox命令行参数引用在这里 ,但是,简单地说,你想要的是一个firefox.exe -new-window <url> (当然使用你想要的URL代替<url> ),然后是一个或多个firefox.exe -new-tab <url> (同上)。 You may also want to control width and height, use a different profile from the default one, etc -- the command-line arguments let you do all that. 您可能还想控制宽度和高度,使用默认配置文件中的其他配置文件等等 - 命令行参数允许您执行所有操作。

In python 3.6, a complete answer will include both webbrowser.open_new() and webbrowser.open_new_tab() from the webbrowser docs . 在python 3.6中,完整的答案将包括来自webbrowser docs的 webbrowser.open_new()和webbrowser.open_new_tab()。

import webbrowser

def main():
    # print(webbrowser._browsers) # for Python 3.x to determine .get() arg
    browser = webbrowser.get('firefox')

    urls = ['url1', 'url2', 'url3']

    first = True
    for url in urls:
        if first:
            browser.open_new(url)
            first = False
        else:
            browser.open_new_tab(url)

if __name__ == '__main__':
    main()

Enjoy the code. 享受代码。 +1 if it helped you out. +1,如果它帮助你。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium - 在新选项卡而不是新窗口中打开 Firefox 链接? Python - Selenium - Open firefox links in new tabs not new window? Python 如何从 Python 或 CLI 在 Firefox 中打开新选项卡? - How to open new tabs in Firefox from Python or CLI? 如何使用 Python 启动 Window 的快捷方式 - How to launch a Window's shortcut using Python 如何使用预期条件 new_window_is_opened 切换选项卡并使用 Selenium 和 Python 验证页面标题 - How to switch tabs using the expected conditions new_window_is_opened and verify the page title using Selenium and Python 如何使用python控制启动应用程序中的按钮或选项卡 - how to control the buttons or tabs in the launch application using python Ubuntu上的Python Selenium Firefox:新选项卡不起作用 - Python Selenium Firefox on Ubuntu: New Tabs Not Working Python Selenium Firefox:使用 Z763F7F1AEC1D50CD1A29ZC 配置文件时无法打开多个选项卡 - Python Selenium Firefox: Unable to open multiple tabs when using Firefox Profiles 如何使用Selenium Python chromedriver在新标签页中循环浏览多个URL并打开URL - How to loop through multiple url and open urls in new tabs using selenium python chromedriver 从python启动新shell窗口中的程序 - launch program in new shell window from python 使用 Python 在 Firefox (win) 选项卡上启动网页 - Launch a webpage on a Firefox (win) tab using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM