4

I'm writing a Python Script using webbrowser module to automatically open the desired webpages.
The issue I'm facing is that I'm only able to open the webpages on different Browser windows and not on the same Browser window on different tabs.

Below is the code that I'm using.

#! /usr/bin/python -tt

import webbrowser

def main():

    webbrowser.open('url1')
    webbrowser.open('url2')
    webbrowser.open('url3')
if __name__ == '__main__':
    main()

I want to open all these links on the same web browser window on separate tabs and not on different browser windows. Thanks :)

3 Answers 3

5

You need to use webbrowser.open_new_tab(url). For example...

import webbrowser
url = 'http://www.stackoverflow.com'
url2 = 'http://www.stackexchange.com'

def main():
    webbrowser.open(url2) # To open new window
    print('Opening Stack Exchange website!')
    webbrowser.open_new_tab(url) # To open in new tab
    print('Opening Stack Overflow website in a new tab!')

if __name__ == '__main__':
    main()
Sign up to request clarification or add additional context in comments.

1 Comment

Waiting for 3 mins to get over. It won't allow me to do before 3 mins. I just checked in ;)
3

In python 3.6, a complete answer will include both webbrowser.open_new() and webbrowser.open_new_tab() from the webbrowser docs.

import webbrowser

def main():
    # print(webbrowser._browsers) # for Python 3.x to determine .get() arg
    browser = webbrowser.get('firefox')

    urls = ['url1', 'url2', 'url3']

    first = True
    for url in urls:
        if first:
            browser.open_new(url)
            first = False
        else:
            browser.open_new_tab(url)

if __name__ == '__main__':
    main()

Enjoy the code. +1 if it helped you out. Cheers!

Comments

2

Simply webbrowser.open_new_tab('url')

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.