tags:

views:

61

answers:

1

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,

+1  A: 

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)
Michael Greene
Thank you. I have a large degree of control over the browser and how it's configured. Have you seen any browser configuration where new=1will work?
golliher
No, I just tried on Ubuntu/Firefox and Win7/Chrome with a few different settings. This Python bug report of a similar issue claims that `new` cannot work on Windows but I can't get it to work on Ubuntu either: http://bugs.python.org/issue1753371
Michael Greene
Ok, you inspired me to read webbrowser.py (surprisingly readable compared to my experience trying to read CPAN modules)... looks like there isn't any signal sent to open in existing window on Mac OSX _unless_ you call get() and specify a browser first. I'll try that next
golliher