Featured image of post Open Browser in Python

Open Browser in Python

We can use webbrowser module to open browser in Python.

Command line:

python -m webbrowser -t "<URL>"

.py script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
url = '<URL>'

# Open straightly (default)
webbrowser.open(url)

# Open in new tab
webbrowser.open_new_tab(url)

# Open in new window
webbrowser.open_new(url)

# Assign a browser(open_new、open_new_tab support this way too)
webbrowser.get('<Browser Code>').open(url)

The browser codes is among mozilla firefox (Mozilla Firefox) opera (Opera) windows-default (Windows default browser) macosx (Mac OS default browser) safari (Safari)google-chrome chrome (Chrome) chromium chromium-browser (Chromium and other browsers based on chromium)

References:

  1. https://docs.python.org/3.7/library/webbrowser.html
Built with Hugo
Theme Stack designed by Jimmy