(window.webpackJsonp=window.webpackJsonp||[]).push([[2844],{3252:function(e,t,s){"use strict";s.r(t);var r=s(31),o=Object(r.a)({},(function(){var e=this,t=e.$createElement,s=e._self._c||t;return s("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[s("h1",{attrs:{id:"webbrowser-module"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#webbrowser-module"}},[e._v("#")]),e._v(" Webbrowser Module")]),e._v(" "),s("p",[e._v("According to Python's standard documentation, the webbrowser module provides a high-level interface to allow displaying Web-based documents to users. This topic explains and demonstrates proper usage of the webbrowser module.")]),e._v(" "),s("h2",{attrs:{id:"opening-a-url-with-default-browser"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#opening-a-url-with-default-browser"}},[e._v("#")]),e._v(" Opening a URL with Default Browser")]),e._v(" "),s("p",[e._v("To simply open a URL, use the "),s("code",[e._v("webbrowser.open()")]),e._v(" method:")]),e._v(" "),s("div",{staticClass:"language-py extra-class"},[s("pre",{pre:!0,attrs:{class:"language-py"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("import")]),e._v(" webbrowser\nwebbrowser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[e._v("open")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[e._v('"http://stackoverflow.com"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),e._v("\n\n")])])]),s("p",[e._v("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:")]),e._v(" "),s("ul",[s("li",[s("code",[e._v("url")]),e._v(" - the URL to open in the web browser (string) "),s("strong",[e._v("[required]")])]),e._v(" "),s("li",[s("code",[e._v("new")]),e._v(" - 0 opens in existing tab, 1 opens new window, 2 opens new tab (integer) "),s("strong",[e._v("[default 0]")])]),e._v(" "),s("li",[s("code",[e._v("autoraise")]),e._v(" - if set to True, the window will be moved on top of other applications' windows (Boolean) "),s("strong",[e._v("[default False]")])])]),e._v(" "),s("p",[e._v("Note, the "),s("code",[e._v("new")]),e._v(" and "),s("code",[e._v("autoraise")]),e._v(" arguments rarely work as the majority of modern browsers refuse these commmands.")]),e._v(" "),s("p",[e._v("Webbrowser can also try to open URLs in new windows with the "),s("code",[e._v("open_new")]),e._v(" method:")]),e._v(" "),s("div",{staticClass:"language-py extra-class"},[s("pre",{pre:!0,attrs:{class:"language-py"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("import")]),e._v(" webbrowser\nwebbrowser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),e._v("open_new"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[e._v('"http://stackoverflow.com"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),e._v("\n\n")])])]),s("p",[e._v("This method is commonly ignored by modern browsers and the URL is usually opened in a new tab.\nOpening a new tab can be tried by the module using the "),s("code",[e._v("open_new_tab")]),e._v(" method:")]),e._v(" "),s("div",{staticClass:"language-py extra-class"},[s("pre",{pre:!0,attrs:{class:"language-py"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("import")]),e._v(" webbrowser\nwebbrowser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),e._v("open_new_tab"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[e._v('"http://stackoverflow.com"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),e._v("\n\n")])])]),s("h2",{attrs:{id:"opening-a-url-with-different-browsers"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#opening-a-url-with-different-browsers"}},[e._v("#")]),e._v(" Opening a URL with Different Browsers")]),e._v(" "),s("p",[e._v("The webbrowser module also supports different browsers using the "),s("code",[e._v("register()")]),e._v(" and "),s("code",[e._v("get()")]),e._v(" methods. The get method is used to create a browser controller using a specific executable's path and the register method is used to attach these executables to preset browser types for future use, commonly when multiple browser types are used.")]),e._v(" "),s("div",{staticClass:"language-py extra-class"},[s("pre",{pre:!0,attrs:{class:"language-py"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("import")]),e._v(" webbrowser\nff_path "),s("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" webbrowser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),e._v("get"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[e._v('"C:/Program Files/Mozilla Firefox/firefox.exe"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),e._v("\nff "),s("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" webbrowser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),e._v("get"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),e._v("ff_path"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),e._v("\nff"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[e._v("open")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[e._v('"http://stackoverflow.com/"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),e._v("\n\n")])])]),s("p",[e._v("Registering a browser type:")]),e._v(" "),s("div",{staticClass:"language-py extra-class"},[s("pre",{pre:!0,attrs:{class:"language-py"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("import")]),e._v(" webbrowser\nff_path "),s("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" webbrowser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),e._v("get"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[e._v('"C:/Program Files/Mozilla Firefox/firefox.exe"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),e._v("\nff "),s("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" webbrowser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),e._v("get"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),e._v("ff_path"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),e._v("\nwebbrowser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),e._v("register"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[e._v("'firefox'")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(",")]),e._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[e._v("None")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(",")]),e._v(" ff"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),e._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[e._v("# Now to refer to use Firefox in the future you can use this")]),e._v("\nwebbrowser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),e._v("get"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[e._v("'firefox'")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[e._v("open")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[e._v('"https://stackoverflow.com/"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),e._v("\n\n")])])]),s("h4",{attrs:{id:"syntax"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#syntax"}},[e._v("#")]),e._v(" Syntax")]),e._v(" "),s("ul",[s("li",[s("code",[e._v("webbrowser.open(url, new=0, autoraise=False)")])]),e._v(" "),s("li",[s("code",[e._v("webbrowser.open_new(url)")])]),e._v(" "),s("li",[s("code",[e._v("webbrowser.open_new_tab(url)")])]),e._v(" "),s("li",[s("code",[e._v("webbrowser.get(usage=None)")])]),e._v(" "),s("li",[s("code",[e._v("webbrowser.register(name, constructor, instance=None)")])])]),e._v(" "),s("h4",{attrs:{id:"parameters"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#parameters"}},[e._v("#")]),e._v(" Parameters")]),e._v(" "),s("table",[s("thead",[s("tr",[s("th",[e._v("Parameter")]),e._v(" "),s("th",[e._v("Details")])])]),e._v(" "),s("tbody",[s("tr",[s("td",[s("code",[e._v("webbrowser.open()")])]),e._v(" "),s("td")]),e._v(" "),s("tr",[s("td",[e._v("url")]),e._v(" "),s("td",[e._v("the URL to open in the web browser")])]),e._v(" "),s("tr",[s("td",[e._v("new")]),e._v(" "),s("td",[e._v("0 opens the URL in the existing tab, 1 opens in a new window, 2 opens in new tab")])]),e._v(" "),s("tr",[s("td",[e._v("autoraise")]),e._v(" "),s("td",[e._v("if set to True, the window will be moved on top of the other windows")])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("webbrowser.open_new()")])]),e._v(" "),s("td")]),e._v(" "),s("tr",[s("td",[e._v("url")]),e._v(" "),s("td",[e._v("the URL to open in the web browser")])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("webbrowser.open_new_tab()")])]),e._v(" "),s("td")]),e._v(" "),s("tr",[s("td",[e._v("url")]),e._v(" "),s("td",[e._v("the URL to open in the web browser")])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("webbrowser.get()")])]),e._v(" "),s("td")]),e._v(" "),s("tr",[s("td",[e._v("using")]),e._v(" "),s("td",[e._v("the browser to use")])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("webbrowser.register()")])]),e._v(" "),s("td")]),e._v(" "),s("tr",[s("td",[e._v("url")]),e._v(" "),s("td",[e._v("browser name")])]),e._v(" "),s("tr",[s("td",[e._v("constructor")]),e._v(" "),s("td",[e._v("path to the executable browser ("),s("a",{attrs:{href:"http://stackoverflow.com/questions/24873302/python-generic-webbrowser-get-open-for-chrome-exe-does-not-work",target:"_blank",rel:"noopener noreferrer"}},[e._v("help"),s("OutboundLink")],1),e._v(")")])]),e._v(" "),s("tr",[s("td",[e._v("instance")]),e._v(" "),s("td",[e._v("An instance of a web browser returned from the "),s("code",[e._v("webbrowser.get()")]),e._v(" method")])])])]),e._v(" "),s("h4",{attrs:{id:"remarks"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#remarks"}},[e._v("#")]),e._v(" Remarks")]),e._v(" "),s("p",[e._v("The following table lists predefined browser types. The left column are names that can be passed into the "),s("code",[e._v("webbrowser.get()")]),e._v(" method and the right column lists the class names for each browser type.")]),e._v(" "),s("table",[s("thead",[s("tr",[s("th",[e._v("Type Name")]),e._v(" "),s("th",[e._v("Class Name")])])]),e._v(" "),s("tbody",[s("tr",[s("td",[s("code",[e._v("'mozilla'")])]),e._v(" "),s("td",[s("code",[e._v("Mozilla('mozilla')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'firefox'")])]),e._v(" "),s("td",[s("code",[e._v("Mozilla('mozilla')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'netscape'")])]),e._v(" "),s("td",[s("code",[e._v("Mozilla('netscape')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'galeon'")])]),e._v(" "),s("td",[s("code",[e._v("Galeon('galeon')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'epiphany'")])]),e._v(" "),s("td",[s("code",[e._v("Galeon('epiphany')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'skipstone'")])]),e._v(" "),s("td",[s("code",[e._v("BackgroundBrowser('skipstone')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'kfmclient'")])]),e._v(" "),s("td",[s("code",[e._v("Konqueror()")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'konqueror'")])]),e._v(" "),s("td",[s("code",[e._v("Konqueror()")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'kfm'")])]),e._v(" "),s("td",[s("code",[e._v("Konqueror()")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'mosaic'")])]),e._v(" "),s("td",[s("code",[e._v("BackgroundBrowser('mosaic')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'opera'")])]),e._v(" "),s("td",[s("code",[e._v("Opera()")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'grail'")])]),e._v(" "),s("td",[s("code",[e._v("Grail()")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'links'")])]),e._v(" "),s("td",[s("code",[e._v("GenericBrowser('links')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'elinks'")])]),e._v(" "),s("td",[s("code",[e._v("Elinks('elinks')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'lynx'")])]),e._v(" "),s("td",[s("code",[e._v("GenericBrowser('lynx')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'w3m'")])]),e._v(" "),s("td",[s("code",[e._v("GenericBrowser('w3m')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'windows-default'")])]),e._v(" "),s("td",[s("code",[e._v("WindowsDefault")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'macosx'")])]),e._v(" "),s("td",[s("code",[e._v("MacOSX('default')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'safari'")])]),e._v(" "),s("td",[s("code",[e._v("MacOSX('safari')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'google-chrome'")])]),e._v(" "),s("td",[s("code",[e._v("Chrome('google-chrome')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'chrome'")])]),e._v(" "),s("td",[s("code",[e._v("Chrome('chrome')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'chromium'")])]),e._v(" "),s("td",[s("code",[e._v("Chromium('chromium')")])])]),e._v(" "),s("tr",[s("td",[s("code",[e._v("'chromium-browser'")])]),e._v(" "),s("td",[s("code",[e._v("Chromium('chromium-browser')")])])])])])])}),[],!1,null,null,null);t.default=o.exports}}]);