[英]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.