Automating Web Browsers with Python

Automating Web Browsers with Python

If you use the same tabs in a Web browser every day, it can seem menial and time consuming opening each tab individually. So, today I’m going to show you how to open all of the tabs you use with one double click.

To start off with, we’re going to need to open a text editor. For this, I’m using Visual Studio, but you can use any text editor. Just remember to save the file with a .py extension. For example: Workflow.py

To begin with, we need to import the ‘webbrowser’ module. This module is responsible for launching and controlling Web Browser Functions. If you are working on Windows, the program will use whatever browser is set as the default. In this case, the tabs will open in Firefox.

After importing the ‘webbrowser’ module, it is time to set up opening Firefox. For this demonstration I am going to load https://www.google.co.uk. The code for that is ‘webbrowser.open_new(“https://www.google.co.uk)

Now that we have written the open browser function, it is time to amend it, so that it will open the following web page in a new tab. This is simply done by amending the previous code to add ‘_tab’ on the end like so: webbrowser.open_new_tab(https://www.facebook.com)

To open any additional tabs from here will also use the ‘_new_tab’ function. This can be used to open as many tabs as needed. Printed below is the full program that I have written for this demonstration.

As displayed in the program, the file is saved as ‘WorkFlow.py’, this is a file that I have saved to the desktop. The image on the desktop shows the Python logo to confirm it has been saved as a Python executable file.

To run the program, just double-click and as you can see in the Screenshot below, it has opened the three webpages that we were setup in the program.

Congratulations, you have just automated the opening of various webpages using Python programming.

Leave a comment