Xeo786 wrote: ↑07 May 2022, 02:01
AHK_user wrote: ↑06 May 2022, 13:49
Great work!
May I propose to include this usefull piece of code of mine that downloads the driver automatically if the driver is not found or if the version is wrong. Why bothering figuring out if what the version of chrome is and where to download the driver?
It does some assumptions about the location of the ChromeDriver.exe
and Chrome.exe, but I think they are acceptable.
EDITED POST: It looks in the registry for the location of Chrome.exe
I would include it in the driver definition, if the first parameter of RunDriver is empty, just apply this...
I would have written something like this but Rufaydium supports multiple drives,
Rufaydium fully supports ChromeDriver and EdgeDriver, there are few non W3C commands which are not supported for drives like Firefox's geckodriver, causing methods (based on
getSessions() like
getSessionByUrl() getSessionByTitle()) not work, I need to add auto-download driver supports for multiple driver from different websites and this would complicate stuff for newbies.
btw your work is awesome, Thank you
@Xeo786:
I guess I could also make something similar for Edge, so we have the most popular browsers covered:
Driver := new RunDriver("ChromeDriver") ; check and update ChromeDriver
Driver := new RunDriver("EdgeDriver") ; check and update EdgeDriver
Driver := new RunDriver(A_Scriptdir "\EdgeDriver.exe") ; use specific file
The original function to use a specific file would still exist, but I auto-updating is usefull.
Alternative, I could return the path of the driver if successfull and write an alternative function to replace RunDriver() with RunAutoDriver().
My guess is that this would be more userfriendly, as the users do not need to download the driver manually anymore. It would be nice if it was included in the standard script.
Example of a working RunAutoDriver() that returns the Rufaydium driver (for now only Chrome)
Code: Select all
#SingleInstance force
Dir_Chromedriver := RegexReplace(A_MyDocuments,"^(.*)\\.*","$1") "\AppData\Local\Google\Chrome\chromedriver.exe"
FileGetVersion, Version_ChromeDriver, % Dir_Chromedriver
MsgBox, % Version_ChromeDriver
; ChromeDriver_Update()
return
ChromeDriver_Update(Dir_Chromedriver:=""){
; Function Updates the Chromedriver by checking the versions and downloading the latest chromedriver.
; Written by AHK_User
; Thanks to tmplinshi
Dir_Chromedriver := Dir_Chromedriver="" ? (FileExist(A_ScriptDir "\chromedriver.exe") ? A_ScriptDir "\chromedriver.exe" : "")
Dir_Chromedriver := Dir_Chromedriver="" ? RegexReplace(A_MyDocuments,"^(.*)\\.*","$1") "\AppData\Local\Google\Chrome\chromedriver.exe" : Dir_Chromedriver
SplitPath, Dir_Chromedriver, , Folder_Chromedriver
if FileExist(Dir_Chromedriver){
; Get Chromedriver version
Version_ChromeDriver := RunHide("""" Dir_Chromedriver """ --version")
Version_ChromeDriver := RegexReplace(Version_ChromeDriver, "[^\d]*([\.\d]*).*", "$1")
} else{
Version_ChromeDriver := "Error"
}
RegRead, File_ChromeExe, % "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Chrome.exe"
if (File_ChromeExe=""){
MsgBox, Failed to find the chrome.exe directory`nCurrent thread will exit.
Exit
}
FileGetVersion, Version_Chrome, % File_ChromeExe
Version_Chrome_ := RegexReplace(Version_Chrome, "\.\d+$")
Version_Chromedriver_ := RegexReplace(Version_Chromedriver, "\.\d+$")
; Check if versions are equal
if (Version_Chrome_=Version_Chromedriver_){
; Versions are the same
Return Dir_Chromedriver
MsgBox,68,ChromeDriver,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
if InStr(Version_Chromedriver, "NoSuchKey"){
MsgBox,16,Testing,Error`nVersion_Chromedriver
Exit
}
; 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)
; Clean up the zipfile
FileDelete, % A_ScriptDir "\chromedriver_win32.zip"
return Dir_Chromedriver
}
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
}
RunAutoDriver(Driver:=""){
Driver := ChromeDriver_Update(Driver)
Return new RunDriver(Driver)
}