Solution 1 :

the docs says, new=0, autoraise=True)
Display url using the
default browser. If new is 0, the url is opened in the same browser
window if possible. If new is 1, a new browser window is opened if
possible. If new is 2, a new browser page (“tab”) is opened if
possible. If autoraise is True, the window is raised if possible (note
that under many window managers this will occur regardless of the
setting of this variable).

Nothing here says that it can replace or close the current tab, so I am afraid that you will have to use something like Selenium instead.

Solution 2 :

I’m unfamiliar with the webbrowser tool, but using selenium webdriver the following all stays in the same tab.

# import
from selenium import webdriver
from import By

# browser setup
browser = webdriver.Chrome()

# open url

# click element on page (same tab)

# open new URL (same tab)

Also, some other tools available in selenium are below. These are useful if you want new tabs but need to switch back and forth to “parent” tabs:

# assign tab variables (window_handle = tab apparently)
window = browser.current_window_handle
parent = browser.window_handles[0]
child = browser.window_handles[1]

# switch to new tab

I hope that at least some element of this is helpful.

Problem :

In Python, I would like to initially open a new browser tab and display a web site. The program will be displaying other png images before cycling back to displaying the web site again. It’s like a slide show. I tried to use the statement displayed below to open the web site with the “New” parameter equaling 1 expecting a new tab not to be opened, but every time is called, a new tab opens on the Chrome browser.

Can you tell me what coding is needed to ensure once a tab is opened in the browser, additional calls to won’t open new tabs for the web site?'', new=1)


Comment posted by Emad-ud-deen

Thanks for the useful coding. Using selenium worked for us after I found out how to install it and get hold of the chrome driver and added its locations in the Mac’s path.