最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Python Webbrowser not opening new window - Stack Overflow

programmeradmin1浏览0评论

From what I am reading Python webbrowser documentation you can either use Open() with a new=1 argument or using open_new() should open a new window. Not another tab.

I was working on a bigger project where I needed this functionality. Thought it was something else in the code and slimmed it down to this:

import webbrowser
webbrowser.open(";, new=1, autoraise=True)
webbrowser.open_new(";)

Even with just these 3 lines of code, the problem persist, both of the different functions should open it in a new window. But non of them does it.

Does anyone know why it opens it in a tab rather than a new window?

From what I am reading Python webbrowser documentation you can either use Open() with a new=1 argument or using open_new() should open a new window. Not another tab.

I was working on a bigger project where I needed this functionality. Thought it was something else in the code and slimmed it down to this:

import webbrowser
webbrowser.open("https://stackoverflow", new=1, autoraise=True)
webbrowser.open_new("https://stackoverflow")

Even with just these 3 lines of code, the problem persist, both of the different functions should open it in a new window. But non of them does it.

Does anyone know why it opens it in a tab rather than a new window?

Share Improve this question asked Feb 10 at 17:31 MrPreciseMrPrecise 34 bronze badges 2
  • What platform is this on? On mac, most of the code degenerates to osascript -e 'open location "<URL>"', which ignores the 'new' option entirely. – Anya Shenanigans Commented Feb 10 at 18:31
  • Windows computer, with Google Chrome as standard browser – MrPrecise Commented Feb 10 at 23:19
Add a comment  | 

2 Answers 2

Reset to default 0

In this case, I'm going to look at a copy of the webbrowser.py code, as present on my local system (python 3.13). It will, by default, on windows, attempt to use the following code to launch a browser:

if sys.platform[:3] == "win":
    class WindowsDefault(BaseBrowser):
        def open(self, url, new=0, autoraise=True):
            sys.audit("webbrowser.open", url)
            try:
                os.startfile(url)
            except OSError:
                # [Error 22] No application is associated with the specified
                # file for this operation: '<URL>'
                return False
            else:
                return True

i.e. it completely ignores new and autoraise. The code in question uses os.startfile, without any parameters.

The long and short of this code is that new and autoraise don't do anything if it successfully invokes the default url handler registered on Windows.

Almost all other browsers on Windows are passed through a BackgroundBrowser class, which does the following:

class BackgroundBrowser(GenericBrowser):
    """Class for all browsers which are to be started in the
       background."""

    def open(self, url, new=0, autoraise=True):
        cmdline = [self.name] + [arg.replace("%s", url)
                                 for arg in self.args]
        sys.audit("webbrowser.open", url)
        try:
            if sys.platform[:3] == 'win':
                p = subprocess.Popen(cmdline)
            else:
                p = subprocess.Popen(cmdline, close_fds=True,
                                     start_new_session=True)
            return p.poll() is None
        except OSError:
            return False

again, looking at the code, we see that it completely ignores new and autoraise.

There does appear to be a lot better support on Unix/Linux for the flags, so unfortunately it looks like you're not going to get it to do what you want with the currently implemented code.

According to the documentation it states that it will open in a window that is already open if possible.

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

There is also this which should do the same thing as the webbrowser function

controller.open(url, new=1, autoraise=True)

Display url using the browser handled by this controller. 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.

发布评论

评论列表(0)

  1. 暂无评论