Skip to content
← Back to Community
How to use the Python Webbrowser Module (Builtin)
Profile icon
gatorade322

A guide to Python’s Builtin webbrowser module

Introduction

In Python, webbrowser is a module you can use to open websites. Webbrowser has 3 main functions. Open, open_new, and open_new_tab. They are basically the same. Supposedly, open_new will open a tab in a new window. I have tested that, and it does not work with Chrome, at least for my OS, ChromeOS. But, first let’s get into the code.

Installation

Webbrowser is builtin, so you do not have to install anything.

Importing Module

Since Webbrowser is builtin, you can just start by typing import webbrowser. Now we have webbrowser in. If you are in repl.it, you may have to install the dependencies, using the tab for that. I’m not sure, but I think, since it’s built in, you can just import webbrowser. I don’t know if repl.it supports it. It should, because you can easily open a new tab with html, css, JS, what repl.it is written in.

Opening a link

The first thing you will have to do, is type webbrowser.open([linktext]). You can also do webbrowser.open_new([linktext]). Finally, you can do webbrowser.open_new_tab([linktext]). If you want, you can have the code take input for the link. Then the link imputed will be opened in a tab. Because, it might be confusing, here is an example of a link: https://a-sub-domain.a-domain.an-end-thing/a-page.html.

The full code

Finally, here is the* full code, (I’m assuming you want to get input for the link):

import webbrowser webpage = input(‘Please enter the webbpage here: ’) webbrowser.open(‘https://’, webpage)
Voters
Profile icon
SylcExport
Profile icon
MaximosRosales
Profile icon
V0RT3X21
Profile icon
RhinoRunner
Profile icon
gatorade322
Comments
hotnewtop
Profile icon
gatorade322

Update: This will not work in repl.it, as you will see if you run the repl above. It has some issue, where it opens a window in repl, and it things it's something like a pygame app. So, repl.it users, I'm sorry, this won't work.

Profile icon
Classfied3D

@gatorade322 actually, he made a mistake of using a comma instead of an plus, but it doesn't work anyway

Profile icon
gatorade322

@Classfied3D In python3 you can do a coma.

Profile icon
gatorade322

@Classfied3D Woops. I'll fix that

Profile icon
Classfied3D

@gatorade322 only for methods like print or functions that use *args as a parameter and automatically join the string together for you. Buts thats pretty advanced python and you need to write your own function, so it's much easier to just use the plus operand.

Profile icon
gatorade322

@Classfied3D Yes, your right. I usually use a plus now, but I made this tutorial a while ago. I changed it to a plus, but I don't know if you can see it.

Profile icon
TurkolYusufiy

Learn how to make it fullscreen

Profile icon
Classfied3D

I recommend that you use webbot instead. It works and its a lot easier to work with if you want some kind of password autofill or something.

Profile icon
StudioHawaii

i wish it did work

Profile icon
RhinoRunner

yeah I tried to open moomoo.io and it gave me a big error

Profile icon
RhinoRunner

@gatorade322

Traceback (most recent call last): File "main.py", line 5, in <module> webbrowser.open('https://', webpage) File "/usr/lib/python3.8/webbrowser.py", line 86, in open if browser.open(url, new, autoraise): File "/usr/lib/python3.8/webbrowser.py", line 269, in open raise Error("Bad 'new' parameter to open(); " + webbrowser.Error: Bad 'new' parameter to open(); expected 0, 1, or 2, got moomoo.io
Profile icon
gatorade322

@RhinoRunner, it looks like this is a webbrowser problem. Try openning it in your local Command Line, For example, the Penguin Terminal for Linux, and the Command Prompt, for WindosOS

Profile icon
gatorade322

@RhinoRunner, this doesn't work on the web, because webbrowser is designed to use something like OS, that doesn't work on your website you use for code.

Profile icon
gatorade322

Addition: This was created on a Google Document, becuase I hate that repl.it doesn't save your work. So, if you would like to view this on a G-Doc, then just go to this link: https://docs.google.com/document/d/1L1pyqRbs8LdHRWu6J8bUYqFElgWPRyOZbYXCPlgjpoA/edit