3

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?

Digital Farmer
  • 1,705
  • 5
  • 17
  • 67

4 Answers4

3

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?

HeyMan
  • 1,529
  • 18
  • 32
  • Hi @HeyMan, thanks for the information but from what I understand, this issue quoted of bugs chromium (https://bugs.chromium.org/p/chromium/issues/detail?id=854609) inside this answear, is about macOS and as commented here (https://github.com/python/cpython/issues/92683#issuecomment-1124922710) currently on MacOS is already working because they couldn't reproduce the flaw on that OS, so they specifically quoted the flaw on Windows – Digital Farmer May 14 '22 at 20:06
  • Strange, I cannot get it running on my mac, neither w/ python3.9 nor 3.10.4. BTW it works e.g. using Plotly Dash (e.g. adding a target=blank to https://stackoverflow.com/a/66390747/2606766). But this is from inside a WebApp so I don't think this will really help. – HeyMan May 15 '22 at 09:12
1

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)
  • This model is interesting to keep as an answer to other people's needs (I added an upvote for that), but it has a problem and a limitation that I think it's interesting to add the info in the answer, it doesn't exactly keep the specific tab open, it plays to the the first one that exists, if we have several tabs open, for example, it will not return to the old, but to the first one whatever it is. – Digital Farmer May 20 '22 at 16:04
1

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})

Yarin_007
  • 1,449
  • 1
  • 10
  • 17
  • Hello @Yarin_007, I'm trying to understand how it works to try to get it running, so far I haven't been able to succeed in my attempts. The first option the browser opens but closes in less than 1 second, the second option ```or tell selenium to use your existing browser:``` the code runs (without ending) but even with the browser open nothing happens. I will continue my attempts for the next few hours! – Digital Farmer May 21 '22 at 01:39
  • A question, from what I understand of selenium, the open browser will deliver to the site that it is a bot that is commanding the browser and not a common user, correct? If so, I will have problems that will result in blocking my access, unfortunately. But I'll keep testing to understand how it works! – Digital Farmer May 21 '22 at 01:44
  • **Edited both options. Please try them (:** Also, I didn't realize you are trying to evade bot-detection. It would really depend on their methods. maybe selenium's behavior will not be enough to simulate a human, maybe not. maybe it's easy as [following some steps](https://piprogramming.org/articles/How-to-make-Selenium-undetectable-and-stealth--7-Ways-to-hide-your-Bot-Automation-from-Detection-0000000017.html). Anyway, if you've got CAPTCHAs or anything similar neither of the solutions proposed is gonna work, you're gonna have to type it in yourself. – Yarin_007 May 21 '22 at 09:27
-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])
Tsefo
  • 409
  • 4
  • 15