Search code examples
python-3.xgoogle-colaboratorypython-webbrowser

Is there any way to open a new tab using a webbrowser module of python in colab?


import webbrowser
webbrowser.open_new_tab('www.google.com')

I am using Google-colaboratory and getting False as a output. Instead of that, it should open a new tab with url of Google. It's working well in python 3.7 as well as in PyCharm.

I know that program written in colab runs on google cloud and there's nothing like browser in google cloud.

Is there any other way to do this in google-colab except by using selenium tools?

Thanks in Advance!


Solution

  • No, there is no way to control your browser via Python's webbrowser package in Colab. The reason is that your browser is running on your local machine, while the Python code in Colab is running on a virtual machine in Google Cloud.

    The only way you can open a new tab via Colab is by injecting javascript code that will cause your browser to do so; for example:

    %%javascript
    window.open('https://colab.research.google.com', '_blank');
    

    But be aware that your browser may block windows opened in this manner.