How to Open URL in Chrome Browser from Python Application?

You can open URL in a browser and also specify to open the URL in Chrome browser specifically.

To open URL in a browser, we will use webbrowser python module.


Follow these steps in your Python Application to open URL in Chrome Browser.

  1. Register the browser type name using webbrowser.register(). Also provide the browser executable file path.
  2. Get the controller object for the browser using webbrowser.get() and Open URL using open()

le 1: Open URL in Google Chrome

In this example, we will open the URL https://pythonexamples.org in Chrome browser.

Python Program

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open(url)


webbrowser.get('chrome').open(url) opens a new window if already not opened, or opens in a new tab if Chrome is already opened in your PC.

Output

Example 2: Open URL in a new Google Chrome Window

At times, there would already be a Chrome browser opened and you would like to open this URL in a new window.

In this example, we will try to open the URL https://pythonexamples.org in a new Chrome browser.

Python Program

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)

open_new() tries to open a new Chrome browser window, only if possible.

Summary

In this tutorial of Python Examples, we learned how to open a URL in Chrome browser.

No comments:

Post a Comment

Featured post

A23 Rummy - Safe Secure Gaming Platform

A23 Rummy is a popular online rummy platform in India. It is owned and operated by Head Digital Works Private Limited. The platform offers...