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

前端 未结 2 337
挽巷
挽巷 2021-01-18 15:08

Given this python code:

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

I would

相关标签:
2条回答
  • 2021-01-18 15:38

    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)
    
    0 讨论(0)
  • 2021-01-18 15:41

    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)
    
    0 讨论(0)
提交回复
热议问题