在Windows OS上打开一个新的在Chrome或Firefox的未关注标签或Firefox的标签
我的操作系统→Microsoft Windows 11
Google Chrome:
我打开了Google网站,我想在新标签中打开堆栈溢出网站,但是屏幕不断显示Google网站,例如:
我的第一次尝试是使用 webbrowser
模块及其 autoraise
参数:
sof = 'https://stackoverflow.com'
webbrowser.open(sof, new=0, autoraise=False)
webbrowser.open(sof, new=2, autoraise=False)
webbrowser.open_new_tab(sof)
没有导致Chrome中的选项卡在后台打开在已经打开的标签上。
因此,我尝试了另一次尝试使用 subprocess
及其 getOutput
函数:
r = subprocess.getoutput(f"google-chrome-stable https://stackoverflow.com")
r
该选项甚至没有在我的浏览器中打开新选项卡。
mozilla firefox:
我的尝试是用 webbrowser
module及其 autoraise
参数(作为我的默认浏览器)是不同的,我需要设置浏览器):
sof = 'https://stackoverflow.com'
webbrowser.register('firefox',
None,
webbrowser.BackgroundBrowser("C://Program Files//Mozilla Firefox//firefox.exe"))
webbrowser.get('firefox').open(sof, new=0, autoraise=False)
在两个我设法使此功能起作用的两个中。
我应该如何继续?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Chrome:
我认为它是不可行的(至少不带Chrome)。
请参阅此stackexchange答案有关详细信息。尤其是最有可能永远无法修复的错误。
firefox:
在这里,做了一些研究,唯一的解决方案是更改配置选项
'browser.tabs.tabs.loaddivertedinbackground'to'true'
这样的启动背景选项卡(或带有
os
或subprocess
模块的PY):“ c:\ program文件\ mozilla firefox \ firefox.exe” -new -tab“ https://stackoverflow.com/”
请参阅 https://stackoverflow.com/a/2276679/2606766 。但是我再次认为这无法解决您的问题,对吗?
Chrome:
I don't think it is feasible (at least not w/ chrome).
See this StackExchange answer for details. Especially the mentioned bug that most likely will never get fixed.
Firefox:
Same here, did some research and the only solution to get it to work is changing the config option
'browser.tabs.loadDivertedInBackground' to 'true'
launch background tab like this (or from py with
os
orsubprocess
module):"C:\Program Files\Mozilla Firefox\firefox.exe" -new-tab "https://stackoverflow.com/"
See https://stackoverflow.com/a/2276679/2606766. But again I don't think this solves your problem, does it?
也许您可以尝试使用pynput库刺激键盘,
然后刺激CRTL +选项卡以更改为新的开放网站?
*编辑:要打开上一个选项卡,请按CRTL + Shift + Tab
maybe you can try to stimulate the keyboard using pynput library,
then stimulating crtl + Tab to change to the new open website?
*edit: to open the previous tab, press crtl + shift + tab
您是否熟悉 cdp 和 selenium ?
选项A:
CDP通过硒控制浏览器:
“ background”:true is 键
编辑:
linux上的浏览器在浏览器上至少对我而言并不关闭。
如果代码死亡时死亡,请尝试以下内容:
选项B:
手动运行带有调试端口的Chrome(通过CMD,
或其他任何东西
subprocess.popen -remote-debugging-port = 4455
,然后使用Python CDP客户端,例如三重奏
或 tell Selenium使用您现有的浏览器:
Are you familiar with CDP and Selenium?
Option A:
CDP Via Selenium Controlled browser:
"background": True is key
EDIT:
On linux the browser doesn't close, at least for me.
If it dies when the code dies, try the following:
Option B:
Manually run chrome with a debug port (via cmd,
subprocess.popen
or anything else)chrome.exe --remote-debugging-port=4455
and then either use a python CDP Client such as trio
or tell selenium to use your existing browser:
最简单的是用Chromedriver切换到-1 Window_handles
Simpliest is to switch to -1 window_handles with chromedriver