Posted by Marta on February 2, 2023 Viewed 17584 times
Hey there! In this tutorial, you will learn how to open a web browser in python. I will show two ways to achieve this, using two different libraries. You will also know what you should consider deciding which library better fit for your solution or use case. Let’s dive in!
The webbrowser
module is a python module that allows opening the web browser. It is a built-in module, which means you don’t need to install anything because the module was installed when you installed python.
If you would like to check if the webbrowser
module is included in your python installation, you can check by executing the following command from your terminal:
python -m webbrowser -t "http://www.google.com"
To open a web browser using the python webbrowser
module, you will first import the module in your python script. Next, you need to call the function .open()
, passing two parameters: the URL of the webpage you would like to open. The second parameter is the new parameter, which indicates how to open the page:
import webbrowser webbrowser.open('http://google.com', new=2)
The code above will open a new tab in your default browser.
Another alternative is opening the browser using the selenium
module. This module allows you to open the web browser and mimic any action, like clicking on buttons, filling forms, scroll down, and any possible action you can do in a browser.
The selenium module is a third-party module, which means you need to install it separately using the pip
tool. You can install it running the following command from your terminal:
pip install selenium
Once you installed selenium, you can open the browser in python using the following code:
from selenium import webdriver your_browser_path = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' driver = webdriver.Chrome(your_browser_path) driver.get("http://www.google.com")
First, you need to import selenium (line 1). Next, you will create an instance of the browser. In this example, I am using the Google Chrome browser; however, selenium supports other browsers such as Edge, Firefox, and Safari. As a parameter, you need to pass the path where your browser is, and the path should include the executable file itself.
Alternatively, if you don’t know where your browser is or can’t find it, you can download a Browser Driver, a browser. You can download a driver from any of the following pages:
This driver is a single file; therefore, once you download it, save it in the same folder where your python script is, and then pass the file path as a parameter to create a web browser instance.
driver = webdriver.Chrome('./chromedriver')
And finally, in line 5, you can use the open()
method, passing as a parameter the page you want to open.
We have seen that you can open a browser with both modules: webbrowser
and selenium
, however which one should you pick? If all you are trying to do is open the browser to display a web page, choose webbrowser
. Selenium is more appropriate if you want to simulate user interactions, like clicking a button or scrolling down, etc. However, using selenium to open a button would be overkill. Nevertheless, if you want to simulate any user action, selenium is your best choice.
Opening the browser using Selenium has some advantages over Webbrowser. One of them is that you could do further actions as login into a webpage.
Selenium will allow you to automate the login step. First, you will need to allocate the widgets to enter your details and then enter the details. In other words, you will write a Selenium bot. Check out the link below to see step by step how to login into a website with Selenium:
To summarise, in this tutorial, you learn how to open a web browser in python using two different python modules: webbrowser
, and selenium
and when to choose each.
I hope you enjoy the tutorial, and thanks for reading and supporting this blog! Happy Coding!
Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications
Unique lay-out and teaching programming style helping new concepts stick in your memory
Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples
Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.
Excellent read for anyone who already know how to program and want to learn Best Practices
Perfect book for anyone transitioning into the mid/mid-senior developer level
Great book and probably the best way to practice for interview. Some really good information on how to perform an interview. Code Example in Java