Checking the version of Chrome if selenium fails to start driver Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
ankitkraken
Posts: 72
Joined: 01 Jun 2019, 08:47
Location: India

Checking the version of Chrome if selenium fails to start driver

21 Sep 2019, 04:36

Hi,

Every time chrome gets updated, I have to manually download the matching chromedriver for selenium and paste it in the selenium directory. I want to create a script which would catch the exception if selenium fails to start the chromedriver and then checks for the chrome version, downloads the latest chromedriver from selenium website and replaces the old driver in the selenium directory with this new downloaded file.

Thanks.
User avatar
ankitkraken
Posts: 72
Joined: 01 Jun 2019, 08:47
Location: India

Re: Checking the version of Chrome if selenium fails to start driver

22 Sep 2019, 07:53

Code: Select all

FileGetVersion, version, %A_ProgramFiles%\AutoHotkey\AutoHotkey.exe
msgbox, %version%
;Try 1
dir := "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
FileGetVersion, version, ‪%dir%
msgbox, %version%
;Try 2
FileGetVersion, version,C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
msgbox, %version%
The first example works but when i try to do the same with chrome its return empty value. Both try1 and 2 don't work for me.
AHKStudent
Posts: 596
Joined: 05 May 2018, 12:23

Re: Checking the version of Chrome if selenium fails to start driver  Topic is solved

22 Sep 2019, 13:39

ankitkraken wrote:
22 Sep 2019, 07:53

Code: Select all

FileGetVersion, version, %A_ProgramFiles%\AutoHotkey\AutoHotkey.exe
msgbox, %version%
;Try 1
dir := "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
FileGetVersion, version, ‪%dir%
msgbox, %version%
;Try 2
FileGetVersion, version,C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
msgbox, %version%
The first example works but when i try to do the same with chrome its return empty value. Both try1 and 2 don't work for me.
Your Try 1 doesn't work because before %dir% there is a "?" hiding

Here is one that works, I removed the hidden ?

Code: Select all

dir := "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
FileGetVersion, version, %dir%
msgbox, %version%
User avatar
ankitkraken
Posts: 72
Joined: 01 Jun 2019, 08:47
Location: India

Re: Checking the version of Chrome if selenium fails to start driver

29 Sep 2019, 04:11

This is working thanks. But how did you know about the hidden "?".
Also how can i know which version of ChromeDriver I am using?
AHKStudent
Posts: 596
Joined: 05 May 2018, 12:23

Re: Checking the version of Chrome if selenium fails to start driver

29 Sep 2019, 06:18

ankitkraken wrote:
29 Sep 2019, 04:11
This is working thanks. But how did you know about the hidden "?".
Also how can i know which version of ChromeDriver I am using?
When I copied and pasted your cope into Scite4Autohotkey I saw the ?

I do not know much about chrome drivers.
tmplinshi
Posts: 1529
Joined: 01 Oct 2013, 14:57

Re: Checking the version of Chrome if selenium fails to start driver

30 Sep 2019, 04:32

Code: Select all

MsgBox % RunHide("""C:\Program Files\SeleniumBasic\chromedriver.exe"" --version")

RunHide(Command) {
	dhw := A_DetectHiddenWindows
	DetectHiddenWindows, On
	Run, %ComSpec%,, Hide, cPid
	WinWait, ahk_pid %cPid%
	DetectHiddenWindows, %dhw%
	DllCall("AttachConsole", "uint", cPid)

	Shell := ComObjCreate("WScript.Shell")
	Exec := Shell.Exec(Command)
	Result := Exec.StdOut.ReadAll()

	DllCall("FreeConsole")
	Process, Close, %cPid%
	Return Result
}
Edit: Fixed RunHide() not hiding cmd window.
User avatar
ankitkraken
Posts: 72
Joined: 01 Jun 2019, 08:47
Location: India

Re: Checking the version of Chrome if selenium fails to start driver

08 Oct 2019, 02:45

Hi,
When trying the above code I'm getting this error. I have changed the directory of the chromedriver to appdata local where it is stored in my system.

Error: 0x80070002 - The system cannot find the file specified.
Source: WshShell.Exec
Description: The system cannot find the file specified.

Edit:
When tried to paste the destination and run from cmd, saw that the path had an exta "?" at the beginning, removed and tried again, it works fine! Thanks a lot!
AHK_user
Posts: 30
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Checking the version of Chrome if selenium fails to start driver

11 Oct 2019, 13:44

Nice code, tmplinshi.
RunHide() is usefull for a lot of projects, I love it.

I combined the code to one function that checks your Chrome and downloads the latest release ChromeDriver.
I am not convinced this is usefull, I have the impression that older ChromeDrivers work most of the time.
But it was a nice exercise as it combines some more advanced functions.

Before running the code you need to fix the lines with "Broken Link for safety" in it (I post this code before my 30th post).
So remove "Broken Link for safety" in those lines and add ":\\" in the line

Updates 2019-10-12:
- Update RunHide function.
- A backup of old chromedriver will now be moved to backup folder.
- chromedriver.exe will now be unzipped in correct folder.

[Mod edit: URLs were fixed]

Code: Select all

SChrome_UpdateDriver()
return

SChrome_UpdateDriver(){
	; Function Updates the Chromedriver by checking the versions and downloading the latest chromedriver.
	; Written by AHK_User
	; Thanks to tmplinshi

	Dir_Chromedriver:= "C:\Users\" A_UserName "\AppData\Local\SeleniumBasic\chromedriver.exe"
	;Dir_Chromedriver:= "C:\Program Files\SeleniumBasic\chromedriver.exe"
	SplitPath, Dir_Chromedriver, , Folder_Chromedriver
	
	FileGetVersion, Version_Chrome, C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
	Version_Chrome := RegexReplace(Version_Chrome, "\.\d+$")
	
	; Get Chromedriver version
	Version_ChromeDriver := RunHide("""" Dir_Chromedriver """ --version")
	;~ DebugWindow("`nVersion Chromedriver:" Version_Chromedriver,Clear:=0,LineBreak:=1)
	Version_ChromeDriver := RegexReplace(Version_ChromeDriver, "[^\d]*([\.\d]*).*", "$1")
	
	;~ DebugWindow("Version Chrome:"  Version_Chrome "`nVersion Chromedriver:" Version_Chromedriver,Clear:=0,LineBreak:=1)
	
	; Check if versions are equal
	if InStr(Version_Chromedriver, Version_Chrome){
		MsgBox,68,Testing,Current Chromedriver is same as Chromeversion.`nDo you still want to download?
		IfMsgBox, No 
		{
			exit
		}
	}
	
; Find the matching Chromedriver
	oHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	oHTTP.Open("GET", "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_"  Version_Chrome, true)
	oHTTP.Send()
	oHTTP.WaitForResponse()
	Version_Chromedriver := oHTTP.ResponseText
	
	;~ DebugWindow("The latest release of Chromedriver is:" Version_ChromeDriver,Clear:=0,LineBreak:=1)
	
	if InStr(Version_Chromedriver, "NoSuchKey"){
		MsgBox,16,Testing,Error`nVersion_Chromedriver
		return
	}
	
; Download the Chromedriver
	Url_ChromeDriver := "https://chromedriver.storage.googleapis.com/" Version_Chromedriver "/chromedriver_win32.zip"
	URLDownloadToFile, %Url_ChromeDriver%,  %A_ScriptDir%/chromedriver_win32.zip
	
; Unzip Chromedriver_win32.zip
	fso := ComObjCreate("Scripting.FileSystemObject")
	AppObj := ComObjCreate("Shell.Application")
	FolderObj := AppObj.Namespace(A_ScriptDir "\chromedriver_win32.zip")
	
	FileCreateDir, Folder_Chromedriver "\Backup"
	FileMoveDir, Dir_Chromedriver, Folder_Chromedriver "\Backup\", 1
	FileObj := FolderObj.ParseName("chromedriver.exe")
	AppObj.Namespace(Folder_Chromedriver "\").CopyHere(FileObj, 4|16)
	
	return
}

RunHide(Command) {
	dhw := A_DetectHiddenWindows
	DetectHiddenWindows, On
	Run, %ComSpec%,, Hide, cPid
	WinWait, ahk_pid %cPid%
	DetectHiddenWindows, %dhw%
	DllCall("AttachConsole", "uint", cPid)

	Shell := ComObjCreate("WScript.Shell")
	Exec := Shell.Exec(Command)
	Result := Exec.StdOut.ReadAll()

	DllCall("FreeConsole")
	Process, Close, %cPid%
	Return Result
}
Last edited by AHK_user on 19 Oct 2019, 00:00, edited 3 times in total.
tmplinshi
Posts: 1529
Joined: 01 Oct 2013, 14:57

Re: Checking the version of Chrome if selenium fails to start driver

12 Oct 2019, 07:54

@AHK_user Sorry actually I posted a not working RunHide function, the cmd window is still visible if the command takes long time, for example RunHide("ping autohotkey.com"). I've fixed it and updated above #p294353.

Thanks for the script, it works for me after two changes:

Code: Select all

	; Version_Chrome := RegexReplace(Version_Chrome, "(.*)\.\d\d", "$1")
	Version_Chrome := RegexReplace(Version_Chrome, "\.\d+$") ; The ending number might have 3 length. Current chrome version is 77.0.3865.120
	
; Get Chromedriver version
	; Version_ChromeDriver := RunHide("""C:\Users\" A_UserName "\AppData\Local\SeleniumBasic\chromedriver.exe"" --version")
	Version_ChromeDriver := RunHide("""C:\Program Files\SeleniumBasic\chromedriver.exe"" --version")

Code: Select all

FindSeleniumInstallPath() {
	o := ComObjCreate("Selenium.ChromeDriver")
	clsid := ComObjType(o, "CLSID")

	regPath := "HKLM\SOFTWARE\Classes\CLSID\" . clsid . "\InprocServer32"
	RegRead, fpath, % regPath, CodeBase
	return RegExReplace(fpath, "\\[^\\]+$")
}

MsgBox, % FindSeleniumInstallPath()
AHK_user
Posts: 30
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Checking the version of Chrome if selenium fails to start driver

12 Oct 2019, 12:00

@tmplinshi: Thanks for the updated RunHide().

I have updated the function of my previous post p296130:
- The new RunHide() function
- chromedriver.exe is overwritten (and moved the old exe file to a backup folder).

On my system, I have a chromedriver.exe file on both directory's, so I am not sure what the correct one is :think:.
Dir_Chromedriver:= "C:\Users\" A_UserName "\AppData\Local\SeleniumBasic\chromedriver.exe"
Dir_Chromedriver:= "C:\Program Files\SeleniumBasic\chromedriver.exe"

Does anybody knows why SeleniumBasic is always installed on a user folder?
The installer does not gives the option to select the install directory.
I would prefer to install it for every user.

The function FindSeleniumInstallPath() is failing on my computer(Windows 10), the register comes back empty.
User avatar
ankitkraken
Posts: 72
Joined: 01 Jun 2019, 08:47
Location: India

Re: Checking the version of Chrome if selenium fails to start driver

18 Oct 2019, 13:17

This is insane :shock:
Thank you so much guys. I probably would have posted a few more questions on the forum to get this task done :D
However,

Code: Select all

oHTTP.Open("GET", "https chromedriver.storage.googleapis.com /LATEST_RELEASE_"  Broken Link for safety Version_Chrome, true)
this line gives me an error.
And can someone explain how this command works. Or point me to a place where I can read about it myself?

Thanks people.
gregster
Posts: 3291
Joined: 30 Sep 2013, 06:48

Re: Checking the version of Chrome if selenium fails to start driver

18 Oct 2019, 14:11

ankitkraken wrote:
18 Oct 2019, 13:17

Code: Select all

oHTTP.Open("GET", "https chromedriver.storage.googleapis.com /LATEST_RELEASE_"  Broken Link for safety Version_Chrome, true)
this line gives me an error.
Just to make sure - Did you fix the URL which was obviously mutilated by the forum software for security reasons?
(the user who originally posted it above didn't have enough posts yet :shh: )

This is better, I guess:

Code: Select all

oHTTP.Open("GET", "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_"  Version_Chrome, true)

Like always, mentioning an unspecified "error" is much less helpful than actually posting the error message that often gives at least a hint of what is wrong.

Open is not an AHK command in this case, but a method of the WinHttp.WinHttpRequest.5.1 COM object that was created in this line: oHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
You can read about this COM object here: https://docs.microsoft.com/en-us/windows/win32/winhttp/winhttprequest
and, especially about the Open method, here: https://docs.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-open

But I am sure, there are also more code and usage examples scattered across the forum.

Hope this helps.
AHK_user
Posts: 30
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Checking the version of Chrome if selenium fails to start driver

18 Oct 2019, 23:57

I am just recently starting to become active on the forum, I will fix it wen I get to my 30th post.
Still 16 to go :D
gregster
Posts: 3291
Joined: 30 Sep 2013, 06:48

Re: Checking the version of Chrome if selenium fails to start driver

19 Oct 2019, 00:32

AHK_user wrote:
18 Oct 2019, 23:57
I am just recently starting to become active on the forum, I will fix it wen I get to my 30th post.
Still 16 to go :D
I don't know, is it 20 or 30..? Well, I can't promote you, but I fixed the two (?) broken links for now. I mean, it's a quality post :thumbup:

Dang, I just saw, you added an explanation to that post - good idea.
But if you like, I can now remove that sentence, too, because if you now edit your post again, these links will probably break again :roll: :) )
Edit: I added a note instead. So, if they should break again, there is still your hint...
User avatar
ankitkraken
Posts: 72
Joined: 01 Jun 2019, 08:47
Location: India

Re: Checking the version of Chrome if selenium fails to start driver

19 Oct 2019, 02:13

gregster wrote:
ankitkraken wrote:
18 Oct 2019, 13:17

Code: Select all

oHTTP.Open("GET", "https chromedriver.storage.googleapis.com /LATEST_RELEASE_"  Broken Link for safety Version_Chrome, true)
this line gives me an error.
Just to make sure - Did you fix the URL which was obviously mutilated by the forum software for security reasons?
(the user who originally posted it above didn't have enough posts yet :shh: )

This is better, I guess:

Code: Select all

oHTTP.Open("GET", "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_"  Version_Chrome, true)

Like always, mentioning an unspecified "error" is much less helpful than actually posting the error message that often gives at least a hint of what is wrong.

Open is not an AHK command in this case, but a method of the WinHttp.WinHttpRequest.5.1 COM object that was created in this line: oHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
You can read about this COM object here: https://docs.microsoft.com/en-us/windows/win32/winhttp/winhttprequest
and, especially about the Open method, here: https://docs.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-open

But I am sure, there are also more code and usage examples scattered across the forum.

Hope this helps.
Thanks so much! I'll read about it.
User avatar
ankitkraken
Posts: 72
Joined: 01 Jun 2019, 08:47
Location: India

Re: Checking the version of Chrome if selenium fails to start driver

19 Oct 2019, 02:22

Duplicate comment
Last edited by ankitkraken on 21 Oct 2019, 22:04, edited 2 times in total.
User avatar
ankitkraken
Posts: 72
Joined: 01 Jun 2019, 08:47
Location: India

Re: Checking the version of Chrome if selenium fails to start driver

21 Oct 2019, 22:01

AHK_user wrote:
11 Oct 2019, 13:44
Nice code, tmplinshi.
RunHide() is usefull for a lot of projects, I love it.

I combined the code to one function that checks your Chrome and downloads the latest release ChromeDriver.
I am not convinced this is usefull, I have the impression that older ChromeDrivers work most of the time.
But it was a nice exercise as it combines some more advanced functions.

Before running the code you need to fix the lines with "Broken Link for safety" in it (I post this code before my 30th post).
So remove "Broken Link for safety" in those lines and add ":\\" in the line

Updates 2019-10-12:
- Update RunHide function.
- A backup of old chromedriver will now be moved to backup folder.
- chromedriver.exe will now be unzipped in correct folder.

[Mod edit: URLs were fixed]

Code: Select all

SChrome_UpdateDriver()
return

SChrome_UpdateDriver(){
	; Function Updates the Chromedriver by checking the versions and downloading the latest chromedriver.
	; Written by AHK_User
	; Thanks to tmplinshi

	Dir_Chromedriver:= "C:\Users\" A_UserName "\AppData\Local\SeleniumBasic\chromedriver.exe"
	;Dir_Chromedriver:= "C:\Program Files\SeleniumBasic\chromedriver.exe"
	SplitPath, Dir_Chromedriver, , Folder_Chromedriver
	
	FileGetVersion, Version_Chrome, C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
	Version_Chrome := RegexReplace(Version_Chrome, "\.\d+$")
	
	; Get Chromedriver version
	Version_ChromeDriver := RunHide("""" Dir_Chromedriver """ --version")
	;~ DebugWindow("`nVersion Chromedriver:" Version_Chromedriver,Clear:=0,LineBreak:=1)
	Version_ChromeDriver := RegexReplace(Version_ChromeDriver, "[^\d]*([\.\d]*).*", "$1")
	
	;~ DebugWindow("Version Chrome:"  Version_Chrome "`nVersion Chromedriver:" Version_Chromedriver,Clear:=0,LineBreak:=1)
	
	; Check if versions are equal
	if InStr(Version_Chromedriver, Version_Chrome){
		MsgBox,68,Testing,Current Chromedriver is same as Chromeversion.`nDo you still want to download?
		IfMsgBox, No 
		{
			exit
		}
	}
	
; Find the matching Chromedriver
	oHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	oHTTP.Open("GET", "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_"  Version_Chrome, true)
	oHTTP.Send()
	oHTTP.WaitForResponse()
	Version_Chromedriver := oHTTP.ResponseText
	
	;~ DebugWindow("The latest release of Chromedriver is:" Version_ChromeDriver,Clear:=0,LineBreak:=1)
	
	if InStr(Version_Chromedriver, "NoSuchKey"){
		MsgBox,16,Testing,Error`nVersion_Chromedriver
		return
	}
	
; Download the Chromedriver
	Url_ChromeDriver := "https://chromedriver.storage.googleapis.com/" Version_Chromedriver "/chromedriver_win32.zip"
	URLDownloadToFile, %Url_ChromeDriver%,  %A_ScriptDir%/chromedriver_win32.zip
	
; Unzip Chromedriver_win32.zip
	fso := ComObjCreate("Scripting.FileSystemObject")
	AppObj := ComObjCreate("Shell.Application")
	FolderObj := AppObj.Namespace(A_ScriptDir "\chromedriver_win32.zip")
	
	FileCreateDir, Folder_Chromedriver "\Backup"
	FileMoveDir, Dir_Chromedriver, Folder_Chromedriver "\Backup\", 1
	FileObj := FolderObj.ParseName("chromedriver.exe")
	AppObj.Namespace(Folder_Chromedriver "\").CopyHere(FileObj, 4|16)
	
	return
}

RunHide(Command) {
	dhw := A_DetectHiddenWindows
	DetectHiddenWindows, On
	Run, %ComSpec%,, Hide, cPid
	WinWait, ahk_pid %cPid%
	DetectHiddenWindows, %dhw%
	DllCall("AttachConsole", "uint", cPid)

	Shell := ComObjCreate("WScript.Shell")
	Exec := Shell.Exec(Command)
	Result := Exec.StdOut.ReadAll()

	DllCall("FreeConsole")
	Process, Close, %cPid%
	Return Result
}

Code: Select all

FileCreateDir, Folder_Chromedriver "\Backup"
"Folder in use, action can't be completed because folder or file is open or in use"

I'm getting this error when the program reaches the above line. Selenium folder is not open anywhere.
AHK_user
Posts: 30
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Checking the version of Chrome if selenium fails to start driver

21 Oct 2019, 23:44

"Folder in use, action can't be completed because folder or file is open or in use"

I'm getting this error when the program reaches the above line. Selenium folder is not open anywhere.
Maybe selenium is running?
Try to close it before running the script.
User avatar
ankitkraken
Posts: 72
Joined: 01 Jun 2019, 08:47
Location: India

Re: Checking the version of Chrome if selenium fails to start driver

22 Oct 2019, 08:14

AHK_user wrote:
21 Oct 2019, 23:44
"Folder in use, action can't be completed because folder or file is open or in use"

I'm getting this error when the program reaches the above line. Selenium folder is not open anywhere.
Maybe selenium is running?
Try to close it before running the script.
After quitting scripts and running again, the error doesn't come anymore but the "backup" folder is not being created.

Return to “Ask For Help”

Who is online

Users browsing this forum: Chunjee, flipmania, Google [Bot], Gully, jesusarell, Odlanir, SuperFoobar, ttttt, v3Van and 203 guests