在Windows OS上打开一个新的在Chrome或Firefox的未关注标签或Firefox的标签

发布于 2025-01-27 19:32:54 字数 1470 浏览 4 评论 0 原文

我的操作系统→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)



在两个我设法使此功能起作用的两个中。

我应该如何继续?

My OS → Microsoft Windows 11

GOOGLE CHROME:

I have Google website open and I want to open the Stack Overflow website in a new tab but the screen keeps showing the Google website, like this:

enter image description here

My first attempt was trying it with the webbrowser module and its autoraise argument:

sof = 'https://stackoverflow.com'

webbrowser.open(sof, new=0, autoraise=False)

webbrowser.open(sof, new=2, autoraise=False)

webbrowser.open_new_tab(sof)

None of the above options caused the tab in Chrome to open in the background keeping focus on the tab that was already open.

So I went for another try using subprocess and its getoutput function:

r = subprocess.getoutput(f"google-chrome-stable https://stackoverflow.com")
r

That option didn't even open a new tab in my browser.




MOZILLA FIREFOX:

enter image description here

My attempt was trying it with the webbrowser module and its autoraise argument (As my default browser is different I need to set the browser):

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)



In neither of the two I managed to make this functionality work.

How should I proceed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

捂风挽笑 2025-02-03 19:32:54

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 or subprocess 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?

黑白记忆 2025-02-03 19:32:54

也许您可以尝试使用pynput库刺激键盘,
然后刺激CRTL +选项卡以更改为新的开放网站?

*编辑:要打开上一个选项卡,请按CRTL + Shift + Tab

import webbrowser, time
from pynput.keyboard import Key,Controller
keyboard = Controller()
webbrowser.open("https://www.youtube.com/")
time.sleep(3)
keyboard.press(Key.ctrl)
keyboard.press(Key.shift)
keyboard.press(Key.tab)
keyboard.release(Key.ctrl)
keyboard.release(Key.shift)
keyboard.release(Key.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

import webbrowser, time
from pynput.keyboard import Key,Controller
keyboard = Controller()
webbrowser.open("https://www.youtube.com/")
time.sleep(3)
keyboard.press(Key.ctrl)
keyboard.press(Key.shift)
keyboard.press(Key.tab)
keyboard.release(Key.ctrl)
keyboard.release(Key.shift)
keyboard.release(Key.tab)
顾铮苏瑾 2025-02-03 19:32:54

您是否熟悉 cdp selenium

选项A:

CDP通过硒控制浏览器:

from selenium import webdriver
driver = webdriver.Chrome('/path/bin/chromedriver')

driver.get("https://example.com/")

driver.execute_cdp_cmd(cmd="Target.createTarget",cmd_args={"url": 'https://stackoverflow.com/', "background": True})

“ background”:true is

编辑:

linux上的浏览器在浏览器上至少对我而言并不关闭。
如果代码死亡时死亡,请尝试以下内容:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

CHROME_DRIVER_PATH = '/bin/chromedriver'

chrome_options = Options()
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(CHROME_DRIVER_PATH, chrome_options=chrome_options)

driver.get("https://example.com/")

driver.execute_cdp_cmd(cmd="Target.createTarget",cmd_args={"url": 'https://stackoverflow.com/', "background": True})


选项B:

手动运行带有调试端口的Chrome(通过CMD,其他任何东西

subprocess.popen -remote-debugging-port = 4455

,然后使用Python CDP客户端,例如三重奏

tell Selenium使用您现有的浏览器:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:4455")
# Change chrome driver path accordingly
CHROME_DRIVER_PATH= r"C:\chromedriver.exe"
driver = webdriver.Chrome(CHROME_DRIVER_PATH, chrome_options=chrome_options)

driver.get("https://example.com/")

driver.execute_cdp_cmd(cmd="Target.createTarget",cmd_args={"url": 'https://stackoverflow.com/', "background": True})

Are you familiar with CDP and Selenium?

Option A:

CDP Via Selenium Controlled browser:

from selenium import webdriver
driver = webdriver.Chrome('/path/bin/chromedriver')

driver.get("https://example.com/")

driver.execute_cdp_cmd(cmd="Target.createTarget",cmd_args={"url": 'https://stackoverflow.com/', "background": True})

"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:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

CHROME_DRIVER_PATH = '/bin/chromedriver'

chrome_options = Options()
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(CHROME_DRIVER_PATH, chrome_options=chrome_options)

driver.get("https://example.com/")

driver.execute_cdp_cmd(cmd="Target.createTarget",cmd_args={"url": 'https://stackoverflow.com/', "background": True})


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:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:4455")
# Change chrome driver path accordingly
CHROME_DRIVER_PATH= r"C:\chromedriver.exe"
driver = webdriver.Chrome(CHROME_DRIVER_PATH, chrome_options=chrome_options)

driver.get("https://example.com/")

driver.execute_cdp_cmd(cmd="Target.createTarget",cmd_args={"url": 'https://stackoverflow.com/', "background": True})

梦里兽 2025-02-03 19:32:54

最简单的是用Chromedriver切换到-1 Window_handles

from selenium import webdriver
driver = webdriver.Chrome('chrome/driver/path')
driver.switch_to.window(driver.window_handles[-1])

Simpliest is to switch to -1 window_handles with chromedriver

from selenium import webdriver
driver = webdriver.Chrome('chrome/driver/path')
driver.switch_to.window(driver.window_handles[-1])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文