Skip to content

Using Python’s webbrowser Package to Open a Website URL

language: Python
package: webbrowser
browser: Google Chrome
OS: Windows

In this tutorial I will explain how to use the Python package webbrowser to first: open up a specified browser tab (Google Chrome), and then second: browse automatically to a specified URL.

The webbrowser module provides a high-level interface to allow displaying Web-based documents to users.

A Simple webbrowser Query in Python

As the instructions state, using the open() function does work, and opens the default web browser – usually I would say: “why wouldn’t I want to use Firefox?!” (my default and favorite browser)

import webbrowser as wb
wb.open_new_tab('http://www.google.com')

The above should work for the computer’s default browser. However, what if you want to  to open in Google Chrome?

The proper way to do this is:

import webbrowser as wb
wb.get('chrome %s').open_new_tab('http://www.google.com')

I’ve substituted ‘chrome’ with ‘google-chrome’ – no luck. To be honest, I’m not really sure that I know the difference between ‘chrome’ and ‘google-chrome’, but apparently there is some since they’ve made the two different type names in the webbrowser documentation.

However, doing this didn’t work right off the bat for me. Every time, I would get the error:

Traceback (most recent call last):
File "C:\Python34\programs\a_temp_testing.py", line 3, in <module>
wb.get('google-chrome')
File "C:\Python34\lib\webbrowser.py", line 51, in get
raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser

Adding chrome.exe Folder to the Environment Variables System PATH of your Windows Computer

I had to add the folder for chrome.exe to System PATH. My chrome.exe executable file is found at:

C:\Program Files (x86)\Google\Chrome\Application

You should check whether it is here or not for yourself.

To add this to your Environment Variables System PATH, right click on your Windows icon and go to System. System Control Panel applet (Start – Settings – Control Panel – System). Change advanced settings, or the advanced tab, and select the button there called Environment Varaibles.

identifying how to find environment variables in Windows System

 

Once you click on Environment Variables here, another window will pop up. Scroll through the items, select PATH, and click edit.

editing the PATH item in Environment Variables

 

Once you’re in here, click new to add the folder path to your chrome.exe file. Like I said above, mine was found at:

C:\Program Files (x86)\Google\Chrome\Application

Click save and exit out of there. Then make sure you reboot your computer.

 

Hope this helps!

 

References:

  1. StackOverflow | How to open website in web browser using Python
  2. Windows IT Pro | How to add a folder to Windows Environment Variable System Path

4 thoughts on “Using Python’s webbrowser Package to Open a Website URL”

  1. Hi Ned, I’m new to python and while I managed to make a url open on firefox while my default browser is chrome, I had to pass the firefox path to the get() function in the following manner:

    url = ‘https://www.youtube.com/’
    firefox_path = ‘C:/Program Files (x86)/Mozilla Firefox/firefox.exe %s’
    webbrowser.get(firefox_path).open(url)

    Can you please tell me what the purpose of the ‘%s’ is in this case? (Note: it doesn’t work without the ‘%s’ at the end)

  2. Hi Ned,

    Just wanted to say thanks for posting this. I was hitting a wall in automating a project and this helped me get past it. I hope your current endeavors are going well!

    – Whit

Leave a Reply

Your email address will not be published. Required fields are marked *

16 − 4 =