Search code examples
pythonpython-3.xpython-webbrowser

Webbrowser opens two windows instead of two tabs


I'm trying to open two websites in two tabs in my web browser. What actually happens is that two separate web browser windows are opened.

import webbrowser
webbrowser.open_new('https://www.msn.com')
webbrowser.open_new_tab('https://www.aol.com/')

Solution

  • The issue is likely that browser hasn't finished opening by the time you ask for a new tab. The docs do state that if no browser is open open_new_tab() acts as open_new(), which is why you are seeing two browsers.

    I suggest putting a small delay between the calls:

    import webbrowser
    import time
    webbrowser.open_new(url1)
    time.sleep(1)
    webbrowser.open_new_tab(url2)
    

    Your other option is to poll the running processes and wait until the first instance of the browser appears before asking for a new tab.