webbrowser not opening new windows
Asked Answered
C

5

6

I just got a new job working remotely and I have to start my day by opening a bunch of pages and logging into them. I would love to automate this process as it can be kind of tedious. I would like to leave my personal browsing window alone and open a new window with all of the pages I need. Here is the gist of what I'm trying to do:

import webbrowser
first = True
chromePath = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
URLS = ("first page", "second page", "third page")
for url in URLS:
    if first:
        webbrowser.get(chromePath).open(url)
        first = False
    else:
        webbrowser.open(url, new=2)

For some reason this code is just opening new tabs in my current browser, which is basically the opposite of what I want it to be doing. What is going on?

Citriculture answered 25/7, 2016 at 18:6 Comment(3)
Do you want to open every url in a separate window or launch one new window with all the tabs inside?Zen
New window with all tabs.Citriculture
I suspect you have chrome set up to create tabs for new pages insead of opening a new browser. Perhaps use another browser (firefox) for your automated load set-up?Sestos
Z
2

I don't have Chrome installed, but there seem to be multiple problems:

  1. According to the docs, webbrowser.get expects the name of the browser, not the path.
  2. You should save the return value of webbrowser.get() and use it to open the remaining urls.

import webbrowser
URLS = ("first page", "second page", "third page")
browser= webbrowser.get('chrome')
first= True
for url in URLS:
    if first:
        browser.open_new(url)
        first = False
    else:
        browser.open_new_tab(url)
Zen answered 25/7, 2016 at 18:25 Comment(1)
thanks for the intention, but this is not the solution of the problem. you can concatenate/split methods, but the result is going to be the same than webbrowser.get(chromepPath).open(url). regardsUnexperienced
S
2

May be too late, but probably will help others.

As per the docs, you should try with new=1

webbrowser.open(url, new=0, autoraise=True)

Display url using the default browser. If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible. If autoraise is True, the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).

Doc link: Webbrowser docs

Sudor answered 25/4, 2017 at 17:44 Comment(1)
I saw this in the docs as well, but never saw it change anything. Can anyone else confirm this worked for them?Tacmahack
S
2

I ran into a similar issue. I just used os.system() to open a new instance of chrome. Also I registered the chrome.exe as a new browser using:

#register the browser
chrome_path = "C:\\Users\\cj9250\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path),1)

There is guidance in other posts on using .get to call the path to chrome. Despite trying this method and many other different methods, I just could not get chrome to open a new window and session.

Finally I came to os.system to open a new instance of chrome. So for your code it would look like this:

import webbrowser
import os #use for new instance of chrome

#urls I want to open in array
URLS = (
        "first page", 
        "second page", 
        "third page"
        )

#register the browser
chrome_path = "YOUR PATH TO CHROME\\chrome.exe" #note the double \ syntax
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path),1)

#open new instance of chrome
os.system(r'YOUR PATH TO CHROME\chrome.exe')

#open each url
for url in URLS:
    webbrowser.get('chrome').open(url)

Looking at what you're trying to do, I think that Selenium would be much better suited. I too use a web application daily for work, and I took the time to run through some Selenium tutorials. It has been much simpler for automation.

Sunnisunnite answered 5/7, 2018 at 18:22 Comment(1)
Two notes regarding this. First: you need to take browser path in quotes again in os.system call. That is because path might contain spaces. e.g. os.system(r'"C:\Program Files\...\chrome.exe"'). Second: it is okay to call webbrowser.open_new_tab("<url>") without needing to register a browser first, after you call os.system(...).Urdu
C
2

The solution is much simpler. Assuming that you have standard path ubication for chrome.exe (and your OS is windows), this is the solution (it opens a new chrome window even if you already had one previously opened). subprocess instead of os.system is mandatory bc otherwise it doesn't open a new chrome window if had one already opened. regards

import subprocess
urL         ='https://www.google.com'
chrome_path ="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
child       = subprocess.Popen(chrome_path, shell=True)
Cowey answered 12/5, 2020 at 13:22 Comment(0)
O
0

This was done on a Windows machine.

I was facing basically the same issue; not being able to open a new browsing window and only being able to open new tabs.

Here's a workaround that allowed me to open a new browsing window (in Chrome) with an initial URL and then open subsequent URLs in that window as tabs.

import webbrowser
import subprocess

# Set this appropriately depending on your situation
chrome_path = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
first_url = 'https://www.google.com/'

# This should open a NEW WINDOW with the URL specified above
command = f"cmd /C \"{chrome_path}\" {first_url} --new-window"

# Alternative way that achieves the same result
# command = f"cmd /C start chrome {first_url} --new-window"

subprocess.Popen(command)

new_tab_urls = [
    'https://www.python.org/',
    'https://hackernoon.com/30-jokes-only-programmers-will-get-a901e1cea549'
]

for url in new_tab_urls:
    webbrowser.open_new_tab(url)
Olnay answered 26/12, 2020 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.