Python Language Opening a URL with Default Browser


Example

To simply open a URL, use the webbrowser.open() method:

import webbrowser
webbrowser.open("http://stackoverflow.com")

If a browser window is currently open, the method will open a new tab at the specified URL. If no window is open, the method will open the operating system's default browser and navigate to the URL in the parameter. The open method supports the following parameters:

  • url - the URL to open in the web browser (string) [required]
  • new - 0 opens in existing tab, 1 opens new window, 2 opens new tab (integer) [default 0]
  • autoraise - if set to True, the window will be moved on top of other applications' windows (Boolean) [default False]

Note, the new and autoraise arguments rarely work as the majority of modern browsers refuse these commmands.

Webbrowser can also try to open URLs in new windows with the open_new method:

import webbrowser
webbrowser.open_new("http://stackoverflow.com")

This method is commonly ignored by modern browsers and the URL is usually opened in a new tab. Opening a new tab can be tried by the module using the open_new_tab method:

import webbrowser
webbrowser.open_new_tab("http://stackoverflow.com")