Do you know how to open URL in the web-browser using Python Program? If you are looking for the answer to this question, here in this tutorial. I am going to share simple commands in Python to open URL in browser. You can use it.
First of all, you need to import webbrowser
library. If the library is not installed on your system, you can install it using pip by executing the following command.
pip install webbrowser
Now you are ready to write your Python program to open any website URL in the web browser.
Let’s start writing the code with examples…
How to open URL in browser Using Python?
# importing webbrowser python module
import webbrowser
#Assigning URL to be opened
strURL = "http://www.csestack.org"
#Open url in default browser
webbrowser.open(strURL, new=2)
The new
parameter have special significance.
Run this program.
If you don’t have webbrowser module installed on your system, you will get the following error.
ImportError: No module named webbrowser
In that case, you have to install the module using pip tool.
import webbrowser
webbrowser.get('firefox').open_new_tab('http://www.csestack.org')
#Opens URL in Firefox browser
webbrowser.get('chrome').open_new_tab('http://www.csestack.org')
#Opens URL in Chrome browser
import webbrowser
webbrowser.open_new('http://www.csestack.org')
# opens in default browser and in new window
import webbrowser
webbrowser.open_new_tab('http://www.csestack.org')
# opens in default browser and in new tab
Exploring it Further…
If you want to explore more method available with the module webbrowser
, you can get all the associated method by running dir()
method.
import webbrowser
dir(webbrowser)
How can you become a Good Web developer Using Python?
If you are interested in Web Development or Automation, you can check out some of the interesting articles that may help you to explore your Python skill…
I use Python for Web development many times. Below is a link that depicts a simple scenario to makes my web development activity so easy with Python.
This all about this programming tutorial in Python to open URL in browser. I also mentioned some of the scenarios you can find this tutorial useful. If you have any doubt, head to the comment section below.
Hi Aniruddha,
Just want to mention the copy paste error that crept in.
webbrowser.get(‘firefox’).open_new_tab(‘http://www.csestack.org’)
#Opens URL in Chrome browser
–John
Hey John,
Thanks for informing. Corrected!
Happy to see you here.
Hi
I want to subscribe to the weekly newsletter
How do I open a link in Python?
I have a rest program It plays my favorite music
For example, I want the program to open the home page on YouTube, Then selects the music randomly.
How to do it?
Hi Abdullah,
You can subscribe here for a newsletter.
Make a get request to Youtube webpage and use web scraping to read and manipulate the content.
Hello Aniruddha thanks for sharing that vase knowledge of information. To be I always wanted to learn a little Python programming but haven’t to it yet.
Welcome, Warren!
Hope you find some useful Python coding stuff on our portal.
Happy Pythoning!
Thank you for this interesting post, I have shared it on Twitter.
Thanks, Rosy!
I am seeing that both webbrowser.open_new & webbrowser.open_new_tab are opening new tabs in Google chrome. I am running this in Windows 10 environment
Jaideep, try using the “new” parameter with webbrowser.open(strURL, new=2) method.
What to do if we want our browser to open by itself and then randomly visit a webpage. Also if there is another URL present inside that page, it should also be visited?
You have to use web scraping to find the URL inside the page. And then open the links in new tab recursively.
Hi Aniruddha..
Thanks for sharing the information. really helpful.
I have a task and need help for some ideas and at the same i am new to coding.
How to retrieve the new release version or service pack from any website? for example, MS power BI is releasing different versions and i would like see some notification through a python program. Can you help me out?
Thanks,
Chakri.
Hi Chakri,
If you want to get the information (like MS power BI versions) from others website, you have to learn web scraping. You can write a Python script to call that website and collect and parse the data to find the release date. You can run that Python script after some time interval.
thank you Aniruddha, I’ve been searching all over for this!
Hi Anirudh,
The code is below:
import webbrowser
URL=”https://www.facebook.com/”
webbrowser. get(‘chrome’). open(ULR)
Error I got:
File “C:\Python27\lib\webbrowser.py”, line 52, in get
raise Error(“could not locate runnable browser”)
Error: could not locate runnable browser
Do I need to register/locate the Browser (It is not default browser)?
Hi Surya,
Looks like your system is not detecting the chrome browser to open. Try…
1. press “Win+R”
2. type “chrome”
And check if it is opening the Chrome browser.
Hi, I am new in Python skill. I want to play IP camera in my raspberry pi’s web browser automatically using Python as it is firefox but when it’s open only blank page gets open, nothing to display. If it is possible please, let me inform. Thanks!
Hi Mitesh, it’s really difficult to debug without knowing your code and implementation. You can try opening different web browsers like Chrome. For Raspberry pi, you can seek help from micro Python.
Thanks for your warm response Aniruddha… I am using selenium python for this IP camera & I am getting output with opening my web browser as chrome/firefox but every time I have to gives a allow permission to that web browser (only in chrome, firefox auto start but getting a white blank page). So, Here I have to gives flash permission in my web browser. I am trying but not getting this.
Mitesh, for now, you can uninstall and Firefox so that you can test in chrome browser. And also check, is your firefox a default web browser? Specify browser name while opening tab, so it will open specified browser instead of default one.
Yes…you’re right but in chrome, with raspberry pi 3 I have come to some unknown error that …
Checking for linux32 chromedriver:72.0.3626.69 in cache
There is no cached driver. Downloading new one…
Trying to download new driver from http:// chromedriver (dot) storage.googleapis.com/72.0.3626.69/chromedriver_linux32.zip
Traceback (most recent call last):
File “chroflash.py”, line 11, in
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), chrome_options=options)
File “/usr/local/lib/python3.5/dist-packages/webdriver_manager/chrome.py”, line 19, in install
bin_file = self._file_manager.download_driver(self.driver, path)
File “/usr/local/lib/python3.5/dist-packages/webdriver_manager/cache.py”, line 75, in download_driver
zip_file = self._download_file(driver, path)
File “/usr/local/lib/python3.5/dist-packages/webdriver_manager/cache.py”, line 99, in _download_file
driver.name, driver.get_version(), driver.get_url()))
ValueError: There is no such driver chromedriver with version 72.0.3626.69 by http:// chromedriver (dot) storage.googleapis.com/72.0.3626.69/chromedriver_linux32.zip
Because of this error, I have many issues in chrome. So, Is there any option to auto allow permission in a web browser?
This is strange, Mitesh. I did not try this with raspberry pi. I will try if I get any solution for this.
Hamm… Ok… If you have any answer please, comment to me. Thanks a lot!!
Sure! Will update you if there is anything from me.
Hi Aniruddha, If I use a pop-up of flash player in python code for the web browser. So, is it possible to develop this type of logic in the local IP camera?
Mitesh, try to pop-up flash player through Python in your web browser. Once you are able to do that, I think it is very much possible to deal with IP camera through your browser.
Hello, I want to open either Firefox or chrome, after opening any browser of my choice I want to open either Facebook or Gmail and want to pass my username and password. All I want to do using argument parser in Python.
Can you please help me? I am new to this and stuck on coding.
Hi Ankita,
You can open the Facebook, Gmail and any other website in Firefox or Chrome by mentioning the website name in the API call as per the demonstration in this tutorial.
Unfortunately, you can not pass a username and password. You can save your username and password in the browser itself. So whenever you open Facebook or Gmail, it will show your profile.
Hi Ankita, My name is Om. I know a way to log in using G – Mail. In fact, I even know how to help you send a G – Mail directly through Python. However, I don’t know about Facebook.
This is how to do it for G – Mail:-
#(Copy the code from here)
Tell me if it works, please, and also if there are any errors.
HI Aniruddha Chaudhari,
how to open multiple URLs by one click.
Hi Honey,
You can call webbrowser.open_new() in a loop.
Example:
hi, how do you know the website is open or not my friend?
John, you can check the HTTP return code. If it is 404, website is not reachable.
Thank you! This was great Aniruddha. Now, what if I want to open all urls from a file (csv)?
This is very much possible, Ivan. Basically, you have to club the two tutorials.
1. Reading CSV files into array list (https://www.csestack.org/how-to-read-csv-file-in-python/)
2. Open URLs (This tutorial) by looping over the complete array list
Hi
I want to create a python script that opens a browser detect username and password on a webpage and then fills
how can I do that?
You can do this with Selenium! There is a selenium module in Python. Install it using pip.
Hi,
I want to open a web page from Linux terminal.
How can I do that?
Thank you!
Hey, use the following command in your Linux terminal.
It will open a given URL in your default browser.
hello airudha ji
this is Vinay.
I’m facing the issue for “Web browser ” in help menu
I have created a python Executable file using Pyinstaller Ubuntu16.04 and I have tested ubuntu18.04.
here the UI working is good but the web browser is not working in 18.04.
the “webbrowser –> URL” is working only 16.04
Hi Vinay,
Can you share the exact error you are getting?
Hi Aniruddha!
When I run it, nothing happens. How to fix that?
Hello Peter.
It’s really difficult to identify what went wrong in your case. Did you get any console error?
What’s the module name Mr. Chaudhari?
James, here we are using “webbrowser” Python module. You have to install it using pip tool.
Nice tutorial and very well explained.
Awesome tutorial! Can I open a URL automatically in TOR as well but just substitute ‘chrome’ or ‘firefox’ with ‘tor’ instead?
Thanks, Jessica! It can not trace the TOR browser automatically. You can mention the executable path of the TOR browser in
webbrowser.get()
method.Sir, if I want to open a URL in chrome for all users then how can I do it?
Hello. I can access the website that I want, but when I get out Python doesn’t continue with the script. Do you have any examples of this? Thanks!
Hi Garu, based on my understanding, what you want is that you want to call and open website url asynchronously. You can use Python async function call mechanism for it.