python-chromedriver-binary
python-chromedriver-binary copied to clipboard
detecting os and windows path
Detecting OS and installation path of Chrome on Windows
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?
Hi @andersoncarlosfs ,
any update on this?
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())
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
import winreg with winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, r"Software\Google\Chrome\BLBeacon") as key: version = winreg.QueryValueEx(key, "version")[0]
Done!
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!
Hello @danielkaiser ,
Please, feel free to make suggestions!
Have a nice day,