Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a new unfocused tab in Chrome or Firefox with Python on Windows OS

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?

like image 535
Digital Farmer Avatar asked Dec 30 '25 06:12

Digital Farmer


1 Answers

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?

like image 130
HeyMan Avatar answered Dec 31 '25 20:12

HeyMan