5

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
  • 1
    I have seen something related. If I have Firefox running with no open windows on a Mac, and execute open -a /Applications/Firefox.app doc1.html doc2.html doc3.html, it will open the documents in three new windows. But, if I have a single window open, with no page loaded, then it will open the documents as tabs in that one window. Odd, hm? Commented Jan 4, 2010 at 3:42

2 Answers 2

6

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)
Sign up to request clarification or add additional context in comments.

3 Comments

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=1 will work?
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: bugs.python.org/issue1753371
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
0

I added a delay between successive invocations of webbrowser.open(). Then each was opened in a new tab instead of a separate window (on my Windows 10 machine).

import time
...
time.sleep(0.5)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.