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.
Checking the version of Chrome if selenium fails to start driver Topic is solved
- ankitkraken
- Posts: 84
- Joined: 01 Jun 2019, 08:47
- Location: India
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
- ankitkraken
- Posts: 84
- Joined: 01 Jun 2019, 08:47
- Location: India
Re: Checking the version of Chrome if selenium fails to start driver
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%
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Checking the version of Chrome if selenium fails to start driver Topic is solved
Your Try 1 doesn't work because before %dir% there is a "?" hidingankitkraken wrote: ↑22 Sep 2019, 07:53The 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.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%
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%
- ankitkraken
- Posts: 84
- Joined: 01 Jun 2019, 08:47
- Location: India
Re: Checking the version of Chrome if selenium fails to start driver
This is working thanks. But how did you know about the hidden "?".
Also how can i know which version of ChromeDriver I am using?
Also how can i know which version of ChromeDriver I am using?
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Checking the version of Chrome if selenium fails to start driver
When I copied and pasted your cope into Scite4Autohotkey I saw the ?ankitkraken wrote: ↑29 Sep 2019, 04:11This is working thanks. But how did you know about the hidden "?".
Also how can i know which version of ChromeDriver I am using?
I do not know much about chrome drivers.
Re: Checking the version of Chrome if selenium fails to start driver
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
}
- ankitkraken
- Posts: 84
- Joined: 01 Jun 2019, 08:47
- Location: India
Re: Checking the version of Chrome if selenium fails to start driver
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!
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!
Re: Checking the version of Chrome if selenium fails to start driver
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]
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.
Re: Checking the version of Chrome if selenium fails to start driver
@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:
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()
Re: Checking the version of Chrome if selenium fails to start driver
@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 .
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.
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 .
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.
- ankitkraken
- Posts: 84
- Joined: 01 Jun 2019, 08:47
- Location: India
Re: Checking the version of Chrome if selenium fails to start driver
This is insane
Thank you so much guys. I probably would have posted a few more questions on the forum to get this task done
However,
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.
Thank you so much guys. I probably would have posted a few more questions on the forum to get this task done
However,
Code: Select all
oHTTP.Open("GET", "https chromedriver.storage.googleapis.com /LATEST_RELEASE_" Broken Link for safety Version_Chrome, true)
And can someone explain how this command works. Or point me to a place where I can read about it myself?
Thanks people.
Re: Checking the version of Chrome if selenium fails to start driver
Just to make sure - Did you fix the URL which was obviously mutilated by the forum software for security reasons?ankitkraken wrote: ↑18 Oct 2019, 13:17this line gives me an error.Code: Select all
oHTTP.Open("GET", "https chromedriver.storage.googleapis.com /LATEST_RELEASE_" Broken Link for safety Version_Chrome, true)
(the user who originally posted it above didn't have enough posts yet )
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.
Re: Checking the version of Chrome if selenium fails to start driver
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
Still 16 to go
Re: Checking the version of Chrome if selenium fails to start driver
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
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 )
Edit: I added a note instead. So, if they should break again, there is still your hint...
- ankitkraken
- Posts: 84
- Joined: 01 Jun 2019, 08:47
- Location: India
Re: Checking the version of Chrome if selenium fails to start driver
Thanks so much! I'll read about it.gregster wrote:Just to make sure - Did you fix the URL which was obviously mutilated by the forum software for security reasons?ankitkraken wrote: ↑18 Oct 2019, 13:17this line gives me an error.Code: Select all
oHTTP.Open("GET", "https chromedriver.storage.googleapis.com /LATEST_RELEASE_" Broken Link for safety Version_Chrome, true)
(the user who originally posted it above didn't have enough posts yet )
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.
- ankitkraken
- Posts: 84
- Joined: 01 Jun 2019, 08:47
- Location: India
Re: Checking the version of Chrome if selenium fails to start driver
Duplicate comment
Last edited by ankitkraken on 21 Oct 2019, 22:04, edited 2 times in total.
- ankitkraken
- Posts: 84
- Joined: 01 Jun 2019, 08:47
- Location: India
Re: Checking the version of Chrome if selenium fails to start driver
AHK_user wrote: ↑11 Oct 2019, 13:44Nice 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"
I'm getting this error when the program reaches the above line. Selenium folder is not open anywhere.
Re: Checking the version of Chrome if selenium fails to start driver
Maybe selenium is running?"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.
Try to close it before running the script.
- ankitkraken
- Posts: 84
- Joined: 01 Jun 2019, 08:47
- Location: India
Re: Checking the version of Chrome if selenium fails to start driver
After quitting scripts and running again, the error doesn't come anymore but the "backup" folder is not being created.
Who is online
Users browsing this forum: doodles333, iamMG, Theda and 176 guests