Python Glossary Русский

all Python 3webbrowserfunction

webbrowser.get()

browser controllers

Returns a browser controller object by name ('firefox', 'chrome') or for the default browser; it has open and open_new_tab.

Syntax

controller = webbrowser.get(using=None)

Unlike the module-level open(), which returns False on failure, get() raises webbrowser.Error when the browser cannot be found — this tells "no browser" apart from "did not open". The name may also be a command line with %s: get('firefox --private-window %s'). The default candidate list depends on the OS and the BROWSER environment variable.

Examples

import webbrowser
try:
    webbrowser.get('no-such-browser')
except webbrowser.Error as e:
    print('Error:', e)  # → Error: could not locate runnable browser
import webbrowser
class Recorder(webbrowser.BaseBrowser):  # поддельный браузер: ничего не открывает
    def open(self, url, new=0, autoraise=True):
        print('open', url, 'new =', new)
        return True
webbrowser.register('recorder', Recorder, preferred=True)
b = webbrowser.get('recorder')
print(type(b).__name__)  # → Recorder
b.open_new_tab('https://python.org')  # → open https://python.org new = 2

See also

Open in the full glossarydocs.python.orgReport an inaccuracy