Prabhu Rajan
Prabhu Rajan

Reputation: 21

How do I open a URL in Google Chrome in new tab using Python?

I am using the below code to open a new chrome browser window.When i run the code it is always opening a new page in the existing tab.

import webbrowser
import pyautogui

url = 'google.com'
chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chrome_path))
webbrowser.get('chrome').open_new(url)

print(pyautogui.position())

Could any one suggest how to open a new tab instead?

Upvotes: 2

Views: 6640

Answers (1)

Philip Petrov
Philip Petrov

Reputation: 970

It should be as simple as:

    webbrowser.get('chrome').open_new_tab(url)

In the docs it says:

webbrowser.open_new_tab(url): Open url in a new page (“tab”) of the default browser, if possible, otherwise equivalent to open_new().

And just for reference:

webbrowser.open_new(url): Open url in a new window of the default browser, if possible, otherwise, open url in the only browser window.

Source: https://docs.python.org/3/library/webbrowser.html

Upvotes: 5

Related Questions