1

I am trying the following:

import webbrowser
url = 'http://docs.python.org/'
webbrowser.open_new_tab(url)

The result is a new window that is empty (the url does not get entered in the address line). The browser is Chrome (version 53.0.2785.92, 64-bit) running under Ubuntu 16.04 LTS. The version of Python is 3.5.2.

How can I fix this?

AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68

2 Answers2

0

Try doing the following with your browser window open (UPDATE: Also tried with no browser windows open and the browser settings set to Open my windows and tabs from last time. Both trials tested with Chrome and Firefox)

import webbrowser
webbrowser.open("http://docs.python.org", new=2)

From the documentation

webbrowser.open(url, 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).

Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable.
Akshar
  • 927
  • 9
  • 7
  • Same result: new empty window. I realize that the problem is likely on the browser side, but have no clue as to how to approach fixing it... – AlwaysLearning Dec 27 '16 at 23:12
  • Interesting that even the URL is not being pushed to the new tab/window for you. Tried doing it with python 2 and 3...seems to work for me. I'd have thought that it was the On Startup settings but that shouldn't not push the url to the new window. I'll keep researching – Akshar Dec 27 '16 at 23:16
  • Can you try it with `import webbrowser` `b = webbrowser.get('')` `b.open('http://docs.python.org/')` – Akshar Dec 27 '16 at 23:33
  • I get an error from the call to `webbrowser.get('')`. Python 3.5 gives key error, Python 2.7 gives an index out of range error `File "/usr/lib/python2.7/webbrowser.py", line 84, in _synthesize cmd = browser.split()[0]` – AlwaysLearning Dec 28 '16 at 08:00
0

First, I tried:

>>> webbrowser.get('chrome')

This did not work. The reason is that the executable for Chrome is /usr/bin/google-chrome! So, I went to /usr/bin and issued the following command in the terminal:

sudo ln -s google-chrome chrome

Now, this works:

>>> webbrowser.get('chrome').open_new_tab('http://www.python.org')

P.S. I am still wondering how to get webbrowser.get('') to work. The default browser is set to be Google-Chrome-Stable...

AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68