Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch new Firefox window with multiple tabs using Python

I want to create a MSWindows Python program that would launch a new Firefox window with multiple tabs each time it is run. For example if I want to search "hello", a new window pops out (even if a Firefox window is already open) and then launches a Google and Bing tabs searching for "hello". If I change the keyword to "world", a new browser pops out again with Google and Bing tabs searching for "world".

I've looked at the webbrowser module but couldn't get it to: 1. Launch a new browser when a browser is already open: e.g. webbrowser.open('http://www.google.com',new=1) will instead open a new tab 2. Launch multiple tabs simultaneously in the same window

Appreciate the help.

Thanks.

like image 895
newbie py Avatar asked Oct 26 '25 14:10

newbie py


1 Answers

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!

like image 195
MyopicVisage Avatar answered Oct 29 '25 05:10

MyopicVisage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!