简体   繁体   English

如何在 python 中使用 webbrowser.open 打开多个 chrome 标签

[英]How to open multiple chrome tabs using webbrowser.open in python

I am trying to execute multiple lines of code so that each line opens 1 URL in its own tab.我正在尝试执行多行代码,以便每行在其自己的选项卡中打开 1 URL。 So for 5 lines of code, I want 5 different chrome tabs to open.所以对于 5 行代码,我想要打开 5 个不同的 chrome 选项卡。 However, when I run my code, only the first line executes.但是,当我运行我的代码时,只执行第一行。 In order to fully execute the block, I have to run the code a second time.为了完全执行该块,我必须再次运行代码。 I want all 5 to open on the first try.我希望在第一次尝试时打开所有 5 个。

I have tried different indentations and included an if statement for the first URL, but neither has seemed to help.我尝试了不同的缩进,并为第一个 URL 添加了 if 语句,但似乎都没有帮助。

import webbrowser

webbrowser.get(chrome_path).open(URL1)
webbrowser.get(chrome_path).open_new_tab(URL2)    
webbrowser.get(chrome_path).open_new_tab(URL3)
webbrowser.get(chrome_path).open_new_tab(URL4)
webbrowser.get(chrome_path).open_new_tab(URL5)
webbrowser.get(chrome_path).open_new_tab(URL6)

I expect all 5 of these to open on the first run.我希望所有 5 个都在第一次运行时打开。 Only the first URL opens on the first run.只有第一个 URL 在第一次运行时打开。 I have to run the code again for the other URL's to open.我必须再次运行代码才能打开其他 URL。

For some reason this will work only if there's a tab open in chrome before executing the script.出于某种原因,只有在执行脚本之前在 chrome 中打开了一个选项卡时,这才有效。

You can try this instead:你可以试试这个:

import webbrowser

webbrowser.open(URL)

This will open the URL with default browser so make sure you have set chrome as default in your case.这将使用默认浏览器打开 URL,因此请确保您已将 chrome 设置为默认浏览器。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM