Hrishi
Hrishi

Reputation: 51

Chrome Path File Issue (Not Personal Issue) while using the Webbrowser Module

So I recently wrote a small code to enter a specific URL using webbrowser module. And it worked on my PC. But when I ran the same code on my friend's PC, the webbrowser module failed to open Chrome. Here is my code:

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',None,webbrowser.BackgroundBrowser("C://Program Files(x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open(url)

So on doing some research, we found out that the issue was with the Program Files(x86). The reason why Chrome opened up on my pc is because the Google folder was stored in Program Files(x86) and Program Files on my friend's PC. So my intention is to create a solution for this so that my function runs on any pc without having to worry about this path issue.

On googling we found this info:

Starting soon, Google Chrome will install in the C:\Program Files\ folder by default on Windows if it is a 64-bit installer. Chrome 64-bit versions installed in the C:\Program Files (x86)\ folder will continue to work and will be updated just like before.

So how do we create a code where first we can determine whether the Google folder is in Program Files or Program Files(x86) and then register the path file in webbrowser.BackgroundBrowser parameter. Any help would be appreciated. Thanks in Advance:)

Update: My new code based on Markus Rosjat's solution

def get_executable(search_path, executeable_name):
    for sdir, dir, files in os.walk(search_path):
        if executeable_name in files:
            exe = files[files.index(executeable_name)]
            return os.path.join(sdir, exe)

for path in [ 'C:\\program files',"C:\\program files(x86)"]:
    exe = get_executable(path, 'chrome.exe')
    if exe:
        url = f'https://{website}.com'
        webbrowser.register('chr', None, webbrowser.BackgroundBrowser(exe))
        webbrowser.get('chr').open(url)

The code isn't returning any thing as such.

Upvotes: 0

Views: 726

Answers (1)

Markus Rosjat
Markus Rosjat

Reputation: 146

So to short example on the comments above

import webbrowser

url = 'https://www.google.com/'
webbrowser.open(url)

should try to attempt to open the default browser on a system. If you wanna use register try the windows-default option

a good tarting point shoould be https://docs.python.org/3/library/webbrowser.html

Update

you could get a controller for the browser you want, well the bowser needs to be installed on the system.

import webbrowser

url = 'https://www.google.com/'
controller = webbrowser.get('chrome')
controller.open(url)

if you look in the doc you could iterate over all the possible presets for the get method and use the first controller you get but that might be a little to much. If you are sure there is a chrome then the lines above should be enough.

Update for a simple function to find the path to an executable, it should be ok for this use.

import os

def get_executable(search_path, executeable_name):
    for sdir,dir,files in os.walk(search_path):
        if executeable_name in files:
            exe = files[files.index(executeable_name)]
            return os.path.join(sdir,exe)

exe = get_executable('C:\\program files', 'chrome.exe')

Update for a simply iteration over a list of paths

this is a quick n dirty example

for path in ['C:\\program files(x86)', 'C:\\program files']:
    exe = get_executable(path, 'opera.exe')
    if exe:
        break

you can put as many paths in the list as you like to search in for your exe file

Update for more explicit failure

again this is just an example, its not intended to be very useful but it gives an idea how to go about things. The aim isnt that I solve the problem, its to give you directions to investigate further ;)

import os
import webbrowser


class BrowserNotFoundException(Exception):
    pass


def get_executable(search_path, executable_name):
    for sdir, dir, files in os.walk(search_path):
        if executable_name in files:
            exe = files[files.index(executable_name)]
            return os.path.join(sdir,exe)

def search_paths(paths, executable_name):
    for path in paths:
        exe = get_executable(path, executable_name)
        if exe:
            return exe
    raise BrowserNotFoundException(f'{executable_name} is not in the path searched({paths})')

paths =  ['c:\\program files(x86)', 'c:\\program files']
executable = 'chrome.exe'
url = 'https://google.com'


exe = search_paths(paths,executable)
webbrowser.register('myBrowser', None, webbrowser.BackgroundBrowser(exe))
webbrowser.get('myBrowser').open(url)

cheers

MArkus

Upvotes: 1

Related Questions