2

I'm trying to open a URL in a new window. I can't use selenium because it wouldn't be signed into Google.

My default browser is Chrome using Windows 10 and I already have 3 Chrome windows open each with multiple tabs.

here is my code on Python 3.6:

import webbrowser

url = 'https://google.com'

open_google = webbrowser.open('https://google.com', new=1)
open_google = webbrowser.open_new('https://google.com')

Both of these give me a new tab in my current window instead of a new window. Why is this happening? is it a setting in Chrome?

Aitor
  • 88
  • 1
  • 9
jason
  • 3,811
  • 18
  • 92
  • 147
  • After doing a bit of reading, it seems that there isn't a way around this without using Selenium – Sam Nov 22 '19 at 14:29
  • maybe try `webbrowser.open('https://google.com --new-window')`. OR simply use `subprocess` or `os.system()` instead of `webbrowser` – furas Nov 22 '19 at 14:53

2 Answers2

1

How about opening a new window using os.system or subprocess and then using webbrowser to get the urls/apps to open there.

Something like:

import subprocess
command = "cmd /c start chrome http://www.google.com --new-window"
subprocess.Popen(command, shell=True)

and then doing:

open_google = webbrowser.open('https://google.com', new=1)
Sharath
  • 216
  • 2
  • 11
0

I want to be able to launch different browsers, because they have a different default theme and their displays look different on my computer.

Here is an approach that parameterizes the URL string to dynamically create the URL with the browser name "on the fly".

import subprocess

browser_type = "chrome"  # Replace the value with "edge" or "firefox"

location_url = "https://stackoverflow.com/questions/7521729/how-to-open-a-new-default-browser-window-in-python-when-the-default-is-{browser_type}"

command_fstring = f"cmd /c start firefox {location_url} --new-window"

subprocess.Popen(command_string, shell=True)

To give credit where it is due, this is based on the answer by @Sharath on this page: Web browser chrome, can't open URL in new window, keeps opening URL as a tab

Rich Lysakowski PhD
  • 2,702
  • 31
  • 44