Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening tabs using Webbrowser module in Python

Tags:

python

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 :)

like image 916
harveyD Avatar asked Dec 26 '22 03:12

harveyD


1 Answers

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()
like image 113
Lachlan Avatar answered Jan 09 '23 06:01

Lachlan