Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python webbrowser.open() - setting new=0 to open in the same browser window doesn't work

Tags:

Given this python code:

import webbrowser
webbrowser.open("http://slashdot.org",new=0)
webbrowser.open("http://cnn.com",new=0)

I would expect a browser to open up, load the first website, then load the second website in the same window. However, it opens up in a new window (or new tab depending on which browser I'm using).

Tried on Mac OSX with Safari, Firefox and Chrome and on Ubuntue with Firefox. I'm inclined to believe that new=0 isn't honored. Am I just missing something?

tia,

like image 836
golliher Avatar asked Jan 04 '10 03:01

golliher


1 Answers

Note that the documentation specifically avoids guarantees with the language if possible: http://docs.python.org/library/webbrowser.html#webbrowser.open

Most browser settings by default specify tab behavior and will not allow Python to override it. I have seen it in the past using Firefox and tried your example on Chrome to the same effect.

On Windows, it is not possible to specify the tab behavior at all, as suggested by my comment below. The url opening code ignores new:

if sys.platform[:3] == "win":
    class WindowsDefault(BaseBrowser):
        def open(self, url, new=0, autoraise=True):
            try:
                os.startfile(url)
like image 88
Michael Greene Avatar answered Oct 03 '22 22:10

Michael Greene