Call to operating system to open url? - python

What can I use to call the OS to open a URL in whatever browser the user has as default?
Not worried about cross-OS compatibility; if it works in linux thats enough for me!

Here is how to open the user's default browser with a given url:
import webbrowser
url = "https://www.google.com/"
webbrowser.open(url, new=0, autoraise=True)
Here is the documentation about this functionality. It's part of Python's stdlibs:
http://docs.python.org/library/webbrowser.html
I have tested this successfully on Linux, Ubuntu 10.10.

Personally I really wouldn't use the webbrowser module.
It's a complicated mess of sniffing for particular browsers, which will won't find the user's default browser if they have more than one installed, and won't find a browser if it doesn't know the name of it (eg Chrome).
Better on Windows is simply to use the os.startfile function, which also works on a URL. On OS X, you can use the open system command. On Linux there's xdg-open, a freedesktop.org standard command supported by GNOME, KDE and XFCE.
if sys.platform=='win32':
os.startfile(url)
elif sys.platform=='darwin':
subprocess.Popen(['open', url])
else:
try:
subprocess.Popen(['xdg-open', url])
except OSError:
print 'Please open a browser on: '+url
This will give a better user experience on mainstream platforms. You could fall back to webbrowser on other platforms, perhaps. Though most likely if you're on an obscure/unusual/embedded OS where none of the above work, chances are webbrowser will fail too.

You can use the webbrowser module.
webbrowser.open(url)

Then how about mixing codes of #kobrien and #bobince up:
import subprocess
import webbrowser
import sys
url = 'http://test.com'
if sys.platform == 'darwin': # in case of OS X
subprocess.Popen(['open', url])
else:
webbrowser.open_new_tab(url)

Have a look at the webbrowser module.

Related

Error "-10814" when opening chrome url with webbrowser.open_new()

I run the following commands and receive execution error.
Here is my code:
import webbrowser
test = "chrome://chrome-urls/"
webbrowser.open_new(test)
Error:
0:37: execution error: An error of type -10814 has occurred. (-10814)
What is my mistake? I use Pycharm on MacOs
Well, it seems there isn't much on this error, but what I could find is that -10814 is a kLSApplicationNotFoundErr. It probably means that the OS launch services can't associate the given URL with a default app to open it, which makes sense since you are trying to open an internal chrome URL. It probably won't throw the same error with standard http/https URLs.
This code works fine for me.
import webbrowser
test = 'https://stackoverflow.com/'
ff = webbrowser.get('google-chrome')
ff.open(test)
But this does not.
import webbrowser
test = 'chrome://chrome-urls/'
ff = webbrowser.get('google-chrome')
ff.open(test)
Maybe you cannot open this type of urls.

Python Selenium -> Check whether Firefox 32/64bit or Chrome 32/64bit installed?

I have coded a python skript using a 32bit geckodriver.exe.
I package this script with the geckodriver included as an Pyinstalled .exe file.
I recognized, this .exe doesn't run when somebody uses Firefox 64bit.
And obviously it doesn't either work when somebody doesnt use Firefox at all, but Chrome instead.
So theoretically, yes I can package 2 geckodriver versions (32/64) and 2 chromedriver versions (32/64) into the .exe, but HOW can I find out which browser and which version (32/64) is installed on the system of the user?
It's only windows-system... so is there a way by reading the users registry or so?
Any ideas appreciated.
Thanks!
I think you can do it with the registry as this:
from winreg import HKEY_LOCAL_MACHINE
import winreg
try:
winreg.OpenKey(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe', 0, KEY_ALL_ACCESS).Close()
except FileNotFoundError:
print('Chrome not installed')
And same for firefox
here is a good way to use it in function:
import os
import win32file
from winreg import *
import winreg
CHROME_KEY = 'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe'
def is_chrome_installed():
try:
key = winreg.OpenKey(HKEY_LOCAL_MACHINE, CHROME_KEY, 0, KEY_ALL_ACCESS)
path_dir = QueryValueEx(key, "PATH")[0]
file_arc = win32file.GetBinaryType(os.path.join(path_dir, 'chrome.exe'))
if file_arc == 0:
print('its a 32bit')
elif file_arc == 6:
print('its a 64bit')
return True
except FileNotFoundError:
print('Chrome not installed')
return False
notice that you need to be run as administartor for this to work

Pythons Webbrowser Module Will Never Open a Link in a new Window

I was trying to automate opening multiple user profiles given a list of names on a few different sites but i can not find a way to open a link in a new window meaning i can not sort the different sites i am opening into their own window collection.
here is my code:
import webbrowser
chrome_path="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
firefox_path="C:\\Program Files\\Mozilla Firefox\\Firefox.exe"
strURL = "http://www.python.org"
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path),1)
webbrowser.register('firefox', None,webbrowser.BackgroundBrowser(chrome_path),1)
webbrowser.open(strURL, new=0)
webbrowser.open(strURL, new=1)
webbrowser.open(strURL, new=2)
webbrowser.get('chrome').open(strURL)
webbrowser.get('firefox').open(strURL)
webbrowser.get('chrome').open_new(strURL)
webbrowser.get('firefox').open_new(strURL)
no matter what value i put for new (0, 1, or 2), all that ever happens is it opens a new tab in the last window i clicked on. i have tried all of the other methods that i found in they python documentation for the webbrowser module and everyone online is just saying to use "new=1" or webbroswer.open_new() but neither of those work. and even when i point it at firefox it just goes to chrome.
P.S.
i found a small workaround that i am not totally satisfied with.
import webbrowser
chrome_path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
chrome_path_NW = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s --new-window"
firefox_path = "C:\\Program Files\\Mozilla Firefox\\Firefox.exe"
strURL = "http://www.python.org"
controller = webbrowser.get(chrome_path)
controllerNW = webbrowser.get(chrome_path_NW)
controllerNW.open(strURL, new=0)
controller.open(strURL, new=1)
controller.open(strURL, new=2)
controller.open("www.youtube.com", new=2)
the important thing to look at would be the "chrome_path" variable. i have changed it so it will run as a command and accept arguments. i found some launch arguments for chromium, here, that seem to work from chrome too. "--new-window" will open a new window and i can then open more tabs in that window but this is a total workaround of pythons module that i am not confident won't break if i am trying to use chrome while running this script. if there is any feature where i could group links together to open in specific windows that would be much more useful to me.
I realise this is a bit late but hopefully i can help someone in the future.
Basically you need to use the subprocess module to open up a new window before you load a new webpage
import subprocess
import time
import webbrowser
subprocess.Popen('open -a /Applications/Google\ Chrome.app --new', shell=True)
time.sleep(0.5) # this is to let the app open before you try to load a new page
webbrowser.open(url)

Webbrowser 'directory not found' error

I am having a reoccurring error with the webbrowser module on python. Whenever I run this program, I get a multiple errors.
import webbrowser
google_urls = ["www.gmail.com", "www.youtube.com"]
def open_tabs(url_lists):
for url in url_lists:
webbrowser.open_new_tab(url)
def main():
webbrowser.open("www.google.com", new=2, autoraise=False)
open_tabs(google_urls)
main()
These are the errors I get:
/home/garion/Documents/www.youtube.com: No such file or directory (also 2 more, which are the same, except the are for www.google.com and www.gmail.com)
I am using lubuntu, python IDLE 3.5, and Chromium.
webbrowser.open('https://www.youtube.com')
use the https:// in front.

Do anyone know how to use python webbrowser.open under window

I had try with python with webbrowser.open, but it only work on IE. How to let it open chrome or firefox. I don't want it to open on IE, i wants to be open on Chrome or Firefox. Due to i try many method, but none of them works.
import time
import webbrowser
webbrowser.open('www.google.com')
you need specify your webbrowser's name, detal see webbrowser.get
import webbrowser
webbrowser.open('www.google.com')
a = webbrowser.get('firefox')
a.open('www.google.com') # True
UPDATE
If you have chrome or firefox installed in your computer, do as following:
chrome_path =r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe' # change to your chrome.exe path
# webbrowser is just call subprocess.Popen, so make sure this work in your cmd firstly
# C:\Users\Administrator>C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe www.google.com
# there two way solve your problem
# you have change \ to / in windows
# this seems a bug in browser = shlex.split(browser) in windows
# ['C:UsersAdministratorAppDataLocalGoogleChromeApplicationchrome.exe', '%s']
a = webbrowser.get(r'C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chrome.exe %s')
a.open('www.google.com') #True
# or by register
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe'))
a = webbrowser.get('chrome')
a.open('www.google.com') #True
else you can try selenium, it provide much more functions and only need chromedriver.

Categories

Resources