3

I'm trying to open the broswer and search some urls with the module webbroswer of python, This is my code

import webbrowser

b = webbrowser.get('firefox')
b.open('google.com')
b.open('stackoverflow.com', new=0)

This code works but it opens the urls in two different tabs, i want that it searchs before for google.com and after in the same tab it has to search for stackoverflow.com. I read in the docs that for open a new thab the new parameter has to be set equal to 2 but it is 0 now, why it keeps opening new tabs?

4
  • 1
    new=0 means the website is opened in the same browser window. It doesn't mean it's opened in the same tab. Commented Oct 25, 2019 at 20:43
  • and what can i do for open it in the same tab? Commented Oct 25, 2019 at 20:44
  • Does this answer your question? Python: Change url of web browser using webbrowser-control Commented Dec 9, 2020 at 13:52
  • The doc is not clear for option 0 regarding the tab although I understand that it will "try" to open a new tab. There is a different approach explained here using Selenium stackoverflow.com/a/54269187/8753169 Commented Apr 5, 2022 at 10:46

3 Answers 3

-1

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

The question is "how do I load a new URL in an existing tab?" Neither of these choices answer that.
try this: script = 'open location "%s"' % url.replace('"', '%22')
-1
b.open('stackoverflow.com', new=0)

Your new should be set to 2 not 0.

Comments

-1
import webbrowser
webbrowser.open('google.com', new = 0)

And the docs says:

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

The same question has been answered here.

1 Comment

it does not work for me, new = 0 or new = 1 or new = 2 is not different from each other

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.