python-chromedriver-binary icon indicating copy to clipboard operation
python-chromedriver-binary copied to clipboard

detecting os and windows path

Open andersoncarlosfs opened this issue 1 year ago • 7 comments

Detecting OS and installation path of Chrome on Windows

andersoncarlosfs avatar Mar 24 '23 12:03 andersoncarlosfs

Hi @andersoncarlosfs thnaks for the PR and sorry for the late response - the last days were quite busy for me.

I just had a look at your code and I'm wondering if this approach has any advantages over the much simpler way of querying the windows registry? This code gives the complete version string at least on my system and even does not require the exact location of the Chrome installation:

import winreg
with winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, r"Software\Google\Chrome\BLBeacon") as key:
   version = winreg.QueryValueEx(key, "version")[0]

With the check for Windows/Cygwin you added and your refactored get_major_version lambda this might be sufficient and require much less code to maintain. Could you check if this also works for your use case?

danielkaiser avatar Apr 01 '23 12:04 danielkaiser

Hi @andersoncarlosfs ,

any update on this?

danielkaiser avatar May 13 '23 08:05 danielkaiser

Hello @danielkaiser,

Well, I was testing your suggestion and I found that it does not work if Chromium/Chrome was not installed by a wizard. However, my solution still works in any case. Besides, I found an answer on Stack Overflow proposing a code less complex:

import subprocess
output = subprocess.check_output(
    r'wmic datafile where name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" get Version /value',
    shell=True
)
print(output.decode('utf-8').strip())
Screenshot 2023-05-15 at 11 55 10

andersoncarlosfs avatar May 15 '23 10:05 andersoncarlosfs

Hi @andersoncarlosfs, sorry for the late response, I haven't had access to my windows machine lately to test things.

The issue I see with this solution is that it needs to know the exact file path (for example I have the 64bit version of Chrome installed and thus had to remove (x86)from the path to make it work. chromium probably has a different name for the executable and there might be users with non-standard installation paths as well.

As there seems to be no perfect solution for Windows a combination of the last two discussed options might be the best option. So getting the information from registry and if that fails try to find chrome.exe and get the version from there?

Also I would think it would be better to remove the shell=True from the call in your comment and instead pass the command args as a list:

output = subprocess.check_output(['wmic', 'datafile', 'where', r'name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"', 'get', 'Version', '/value'])

For a hardcoded string this should be no issue but as this part might be changed in the future this could be a problem

danielkaiser avatar Jun 05 '23 16:06 danielkaiser

import winreg
with winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, r"Software\Google\Chrome\BLBeacon") as key:
   version = winreg.QueryValueEx(key, "version")[0]

Done!

andersoncarlosfs avatar Aug 22 '23 06:08 andersoncarlosfs

Hi @andersoncarlosfs, sorry for the late response, I haven't had access to my windows machine lately to test things.

The issue I see with this solution is that it needs to know the exact file path (for example I have the 64bit version of Chrome installed and thus had to remove (x86)from the path to make it work. chromium probably has a different name for the executable and there might be users with non-standard installation paths as well.

As there seems to be no perfect solution for Windows a combination of the last two discussed options might be the best option. So getting the information from registry and if that fails try to find chrome.exe and get the version from there?

Also I would think it would be better to remove the shell=True from the call in your comment and instead pass the command args as a list:

output = subprocess.check_output(['wmic', 'datafile', 'where', r'name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"', 'get', 'Version', '/value'])

For a hardcoded string this should be no issue but as this part might be changed in the future this could be a problem

Done!

andersoncarlosfs avatar Aug 22 '23 06:08 andersoncarlosfs

Hello @danielkaiser ,

Please, feel free to make suggestions!

Have a nice day,

andersoncarlosfs avatar Aug 22 '23 06:08 andersoncarlosfs