2023-07-28 10:15:46 -04:00
; Notes/Extra Info/#Includes
2023-02-28 18:40:06 -05:00
;------------------------------------------------
2023-07-28 10:15:46 -04:00
; Selenium Related Functions
2023-02-28 18:40:06 -05:00
2023-09-17 14:25:54 -04:00
global MouseClicksSaved
2023-03-01 23:08:24 -05:00
ShowSeleniumErrorMsgbox ( ) {
IniRead , PreviousWorkingChromeVersion , Settings . ini , Misc , ChromeVersion , %A_Space%
; IniWrite, %ChromeVersion%, Settings.ini, Misc, ChromeVersion
; msgbox, failed to connect to Chrome for some reason.
; Message = Failed to Connect to Chrome. Most likely problem is either Chrome has some sort of dialogue box open or ChromeDriver needs to be updated.
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
OnMessage ( 0x44 , " OnMsgBoxChromeDriverFailed " )
2023-09-17 00:15:31 -04:00
MsgBox 0x40043 , Error , Failed to Connect to Chrome . `n Possible Causes for this : `n 1 ) One of your tabs is stuck loading / refreshing . ( Try closing all tabs and restarting ) `n 2 ) Chrome has a dialogue box open in one of your tabs . `n 3 ) ChromeDriver needs to be updated . `n `n Click " Restart " to try the Upload again `n Click " ChromeDriver " to open up the ChromeDriver download page . `n `n Click " Instructions " to open up the instructions website for updating Chromedriver . `n `n Possibly Helpful Info : `n Current Chrome Version : %Chromeversion% `n Chrome Version of Last Successfull Upload : %PreviousWorkingChromeVersion%
OnMessage ( 0x44 , " " )
IfMsgBox Yes , {
2023-09-17 14:25:54 -04:00
; Open Instructions Gitea page
2023-09-17 00:15:31 -04:00
if ( InStr ( A_ScriptName , " Video " ) )
2023-12-11 13:27:57 -05:00
URL = https : / / freedomain . dev / yuriy / video - uploader#installation
2023-09-17 00:15:31 -04:00
else ,
2023-12-11 13:27:57 -05:00
URL = https : / / freedomain . dev / yuriy / social - media - poster#installation
2023-09-17 00:15:31 -04:00
run , %URL%
run , " %A_ScriptFullPath% " " LastPost "
}
Else IfMsgBox No , {
; open chromedriver download page
run , https : / / chromedriver . chromium . org / downloads
run , " %A_ScriptFullPath% " " LastPost "
; open chromedriver site
} Else IfMsgBox Cancel , {
; Reload with last post
run , " %A_ScriptFullPath% " " LastPost "
}
ExitApp
}
OnMsgBoxChromeDriverFailed ( ) {
DetectHiddenWindows , On
Process , Exist
If ( WinExist ( " ahk_class #32770 ahk_pid " . ErrorLevel ) ) {
WinMove , , 0
ControlSetText Button1 , Instructions
ControlSetText Button2 , ChromeDriver
ControlSetText Button3 , Restart
}
2023-08-20 00:42:10 -04:00
}
2023-03-01 23:08:24 -05:00
2023-02-28 18:40:06 -05:00
; -------------------------------Tab Navigation & Activation-------------------------------
2023-03-01 23:08:24 -05:00
CheckSeleniumDriver ( ) {
2023-07-28 19:55:55 -04:00
try Driver := ConnectToChrome ( " " , ChromeProfile := " " ) ; open new tab page with with specified profile
2023-03-01 23:08:24 -05:00
catch e {
ShowSeleniumErrorMsgbox ( )
; Message = Failed to Make Connection to Chrome. Check for Any Open Dialogue Boxes or out of date ChromeDriver
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
Return " Failed "
}
2023-07-28 19:55:55 -04:00
; Driver := ConnectToChrome("https://www.bitchute.com/", ChromeProfile) ; open new tab page with with specified profile
2023-03-01 23:08:24 -05:00
; driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
try driver . executeScript ( " return document.readyState " ) . equals ( " complete " ) ; wait until page loads completely before proceeding
catch e {
ShowSeleniumErrorMsgbox ( )
; Message = Failed to send command to Chrome. Check for an Open Dialogue Box.
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
Return " Failed "
}
DriverStatus := 1
CreateArrayOfChromeTabs ( )
}
SeleniumConnectToActiveTab ( IP_Port := " 127.0.0.1:9222 " ) {
Driver := ComObjCreate ( " Selenium.ChromeDriver " )
Driver . SetCapability ( " debuggerAddress " , IP_Port )
try Driver . Start ( )
catch e {
Message = Failed to Connect to Chrome . Showing Error Msgbox
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
ShowSeleniumErrorMsgbox ( )
; msgbox, failed to connect to Chrome for some reason.
; Message = Failed to Connect to Chrome for some reason.
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
}
return Driver
}
/*
* /
CreateArrayOfChromeTabs ( ) {
Message = Creating an Array of All Chrome Tabs
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile " )
; Create an arrays
DriverTitleArray := [ ]
ChromeTabsURLArray := [ ]
try TotalTabsFound := Driver . Windows . Count
2023-03-04 00:13:31 -05:00
StartTime := A_TickCount
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
; 5 is arbitrary number to do some extra loops to make sure all tabs are gone through
; most likely not necessary because unloaded tabs cannot be activated
2023-04-02 23:17:52 -04:00
TotalTabLoops := TotalTabsFound + 2
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
Message = Total Pre - Existing Tabs in Chrome : %TotalTabsFound% `n Total Times looping through tabs to create Chrome Tabs Array : %TotalTabLoops%
SaveOrPostProgress ( Message , PostType := " ErrorLoggingTextFile,DiscordErrorLogging " )
2023-03-01 23:08:24 -05:00
2023-04-02 23:17:52 -04:00
Message = Creating an Array of All Chrome Tabs `n ( Sometimes might get stuck for a short while if acive tab 's page is reloading / loading )
2023-03-04 00:13:31 -05:00
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile " )
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
Loop % TotalTabLoops {
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
try Title := Driver . Title
try URL := Driver . URL
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
if ( Title != " " )
DriverTitleArray . Push ( Title )
if ( URL != " " )
ChromeTabsURLArray . Push ( URL )
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
; exit after looping through 1 tab, if only 1 tab found in count
if ( TotalTabsFound = 1 AND A_index > TotalTabsFound ) {
Message = Exiting Early after 1 loop . Only 1 tab found
SaveOrPostProgress ( Message := Message , PostType := " ErrorLoggingTextFile,DiscordErrorLogging " )
; DevModeMsgBox(message)
break
}
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
TabEndTime := A_TickCount - TabFoundStartTime
TabEndTimeArray .= TabEndTime . " , "
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
; Message = Time to switch to new tab: %TabEndTime%`nCurrent Loop: %A_index%/%TotalTabsFound%
; SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
TabFoundStartTime := A_TickCount
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
; TimeToGrabCurrentTab := A_TickCount - StartTime
; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
; Stop page refresh if it's happening
; try driver.executeScript("return window.stop")
2023-03-01 23:08:24 -05:00
2023-03-04 00:13:31 -05:00
; TimeToGrabCurrentTab := A_TickCount - StartTime
; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab
; VAR := HasVal(ChromeTabsURLArray, STRINGVAR) ; returns position of value in array
2023-03-01 23:08:24 -05:00
/*
if ( HasVal ( ChromeTabsURLArray , URL ) ) { ; returns position of value in array
DevModeMsgBox ( " Skipping rest of loop and continuing " )
Continue
}
* /
try driver . SwitchToNextWindow ;Focuses Selenium on the newly opened/next window.
; DevModeMsgBox("switching to next tab")
} ; end of loop
; TimeToLoopThroughAllTabs := A_TickCount - StartTime
; Message = Time to Loop through all tabs: %TimeToLoopThroughAllTabs%
; SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
; DevModeMsgBox(Message)
return
}
/*
* /
2023-02-28 18:40:06 -05:00
CheckCurrentTabForCurrentSite ( ) {
2023-02-28 22:07:28 -05:00
try CurrentTabURL := GetCurrentTabURlBase ( )
2023-02-28 18:40:06 -05:00
2023-02-28 22:15:43 -05:00
CheckForAlerts ( )
2023-02-28 18:40:06 -05:00
if ( ! InStr ( CurrentTabURL , CurrentSite ) ) {
2023-02-28 22:07:28 -05:00
Message = Chromedriver failed to switch to %CurrentSite% . Current Tab URL : %CurrentTabURL%
2023-08-20 00:36:38 -04:00
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,ErrorSummaryVar " )
2023-02-28 18:40:06 -05:00
return " Failed "
}
2023-03-01 23:18:48 -05:00
else , {
Message = CheckCurrentTabForCurrentSite ( ) passed successfully
2023-08-20 00:36:38 -04:00
SaveOrPostProgress ( Message := Message , PostType := " ErrorLoggingTextFile " )
2023-03-01 23:18:48 -05:00
}
2023-02-28 18:40:06 -05:00
}
/*
* /
2023-03-01 23:08:24 -05:00
GetCurrentTabURLBase ( ) {
2023-02-28 20:42:12 -05:00
try , TabURL := driver . url
TabURL := ExtractBaseURL ( TabURL )
return TabURL
}
2023-02-28 18:40:06 -05:00
ExtractBaseURL ( URL ) {
URLBase := StrReplace ( URL , " https:// " , " " ) ; remove beginning of URL
URLBase := StrReplace ( URLBase , " http:// " , " " ) ; remove beginning of URL
URLBase := StrReplace ( URLBase , " www. " , " " ) ; remove www if there 1
URLBase := StrSplit ( URLBase , " / " ) ; remove text after url base
URLBase := URLBase [ 1 ]
return URLBase
}
NavigateFromBaseURLTo ( URL , PageTitle := " TodoDeleteme " ) {
; if not connected to selenium chrome, then re-connect. This will also generate an array of all open chrome tabs
if ( ! DriverStatus ) {
Status := CheckSeleniumDriver ( )
if ( Status ) {
Message = Failed to Connect to Chrome . Please Check for any open dialog boxes or ChromeDriver being out of date .
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging " )
Return " Failed "
}
}
; extract Base URL from Full URL
URLBase := ExtractBaseURL ( URL )
2023-09-16 21:31:53 -04:00
; Message = Checking for URLBase: %URLBase% within ChromeTabsURLArray
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
2023-03-04 00:10:13 -05:00
2023-04-23 15:19:10 -04:00
; msgbox % HasSubstringVal(ChromeTabsURLArray, URLBase) . "`n`n is the status of URLbase in ChromeTabsURLArray"
2023-02-28 18:40:06 -05:00
; if URLBase is NOT a substring within any of the values in the array
if ( ! HasSubstringVal ( ChromeTabsURLArray , URLBase ) )
{
; DevModeMsgBox("Creating new tab")
2023-09-16 21:31:53 -04:00
Message = Chrome tab does not exist in ChromeTabsURLArray . Creating new tab ;. Creating new tab with URL: %URL%
2023-04-23 15:19:10 -04:00
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
2023-02-28 18:40:06 -05:00
try , run " %ChromeFilepath% " " %URL% "
2023-03-01 23:08:24 -05:00
catch e {
Message = Trying to Run Chrome with URL failed for some reason . Wrong Chrome filepath ?
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
}
2023-02-28 18:40:06 -05:00
ChromeTabsURLArray . Push ( URL ) ; Append the new url to the array
NewTabCreated := 1
sleep , 1000
}
else , {
Message = %URLBase% is within ChromeTabsURLArray . Trying to Activate .
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
}
; message = NewTabCreated: %NewTabCreated%
; DevModeMsgBox(message)
status := ActivateChromeTab ( URLBase )
if ( Status = " Failed " ) {
2023-03-01 23:08:24 -05:00
try currentURL := GetCurrentTabURLBase ( )
2023-04-23 15:19:10 -04:00
Message = Failed to switch active tab to %URLBase% , currently on different site .
2023-04-02 23:17:52 -04:00
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
2023-02-28 18:40:06 -05:00
CheckForAlerts ( )
; create a new tab and try to switch to it again
2023-03-01 23:08:24 -05:00
Message = Creating new Chrome tab with URL : %URL%
2023-02-28 18:40:06 -05:00
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
try , run " %ChromeFilepath% " " %URL% "
2023-03-01 23:08:24 -05:00
catch e {
Message = Trying to Run Chrome with URL failed for some reason . Wrong Chrome filepath ?
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
}
2023-02-28 18:40:06 -05:00
ChromeTabsURLArray . Push ( URL ) ; Append the new url to the array
NewTabCreated := 1
sleep , 1000
2023-02-28 20:42:12 -05:00
status := ActivateChromeTab ( URL )
2023-02-28 18:40:06 -05:00
if ( Status = " Failed " ) {
2023-03-01 23:08:24 -05:00
Message = Failed to activate tab for %urlBase% after 2 seperate attempts . Chrome stuck on a dialog box of some sort ?
2023-02-28 18:40:06 -05:00
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging " )
Return " Failed "
}
}
; message = ActivateChromeTab Status: %status%
; DevModeMsgBox(message)
if ( ! NewTabCreated ) { ; if re-using a tab then we want to re-navigate to URL because it might be the wrong page
; Double check that the current tab URL matches the url base
try , CurrentTabURl := driver . url
; Message = Reusing Tab. `nCurrentTabURl: %CurrentTabURl%`nURL Navigating to: %URL%
; DevModeMsgBox(Message)
; DevModeMsgBox("getting url")
try driver . Get ( URL ) ;Open selected URL
catch e {
Message = Failed to Navigate to URL in pre - existing tab `n Please See Error #5648 for code section
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging " )
; SaveDriverURLOFErrorPage()
; Return
; msgbox, error caught. trying something
try driver . switchToalert ( ) . accept ( )
try driver . Get ( URL ) ;Open selected URL
catch e {
Message = Failed to Navigate to URL : %URL% . Please Check for any open dialog boxes or ChromeDriver being out of date .
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging " )
Return " Failed "
}
}
try driver . executeScript ( " return document.readyState " ) . equals ( " complete " ) ; wait until page loads completely before proceeding
}
Return
}
2023-02-28 20:42:12 -05:00
; -------------------------------ActivateChromeTab-------------------------------
2023-02-28 18:40:06 -05:00
ActivateChromeTab ( URL ) {
2023-02-28 20:42:12 -05:00
; Extract Base URL if necessary
2023-02-28 18:40:06 -05:00
if ( InStr ( URL , " / " ) ) {
URLBase := ExtractBaseURL ( URL )
}
else , {
URLBase := URL
}
; Message = Finding Tab with URL base: %URLBase%
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
; DevModeMsgBox(Message)
; if not connected to selenium chrome, then re-connect
if ( ! DriverStatus ) {
Status := CheckSeleniumDriver ( )
if ( Status )
Return " Failed "
}
TabFoundSuccessfully :=
2023-04-02 23:17:52 -04:00
NumberOfTabActivationLoops := ChromeTabsURLArray . Length ( )
; Message = NumberOfTabActivationLoops has been increased by 3
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
2023-03-01 23:08:24 -05:00
Message = Looping through tabs - > %NumberOfTabActivationLoops% < - times to find tab with URL : %urlBase%
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
try , StartingTabURL := driver . url
2023-02-28 18:40:06 -05:00
; Message := "NumberOfTabActivationLoops: " NumberOfTabActivationLoops
; DevModeMsgBox(Message)
; NumberOfTabLoops := Driver.Windows.Count + 2
2023-03-01 23:08:24 -05:00
; Message = Looping through tabs to activate with: %URLBase%
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
; create empty variable for loop
LastActiveTab :=
2023-02-28 18:40:06 -05:00
loop % NumberOfTabActivationLoops { ; loop through tabs to find matching tab
2023-03-01 23:08:24 -05:00
UStartTime := A_TickCount ; start time
2023-02-28 18:40:06 -05:00
try driver . SwitchToNextWindow ( )
2023-03-01 23:08:24 -05:00
; calculate run time and convert to seconds
URunTime := round ( ( ( A_TickCount - UStartTime ) / 1000 ) , 2 )
if ( URunTime > 5 ) {
Message = Time to switch Tabs took %URunTime% seconds
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
}
; Msgbox % "URunTime: " URunTime
2023-02-28 18:40:06 -05:00
; sleep, 1000
; msgbox, looping through tabs
try CurrentTabTitle := driver . window . title
try CurrentTabURL := driver . Url
2023-03-01 23:08:24 -05:00
if ( A_index < 3 ) {
if ( CurrentTabURL = StartingTabURL ) {
Message = Current Tab URL is THE SAME as Starting URL after %A_index% tab switches `n Stuck on this tab ?
2023-08-20 00:36:38 -04:00
SaveOrPostProgress ( Message := Message , PostType := " ErrorLoggingTextFile " )
2023-03-01 23:08:24 -05:00
CheckForAlerts ( )
}
else , {
Message = Current Tab URL is DIFFERENT from starting URL after %A_index% tab switches
2023-08-20 00:36:38 -04:00
SaveOrPostProgress ( Message := Message , PostType := " ErrorLoggingTextFile " )
2023-03-01 23:08:24 -05:00
}
if ( LastActiveTab = CurrentTabURL ) {
Message = Chrome is on same tab URL after calling SwitchToNextWindow ( ) function
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
CheckForAlerts ( )
}
}
2023-02-28 18:40:06 -05:00
if ( InStr ( CurrentTabURL , URLBase ) ) {
2023-03-01 23:08:24 -05:00
TabURL := GetCurrentTabURLBase ( )
Message = Successfully Found and Activated Tab . URLBase : %urlBase% and currentTabURL : %TabURL%
2023-02-28 18:40:06 -05:00
; Message = Found Tab:.`n%CurrentTabTitle% = %PageTitle%`n%CurrentTabURL% = %URLBase%
; DevModeMsgBox(message)
TabFoundSuccessfully := 1
; msgbox %CurrentTabTitle% = %PageTitle%
; msgbox %CurrentTabURL% = %URL%
return
}
2023-03-01 23:08:24 -05:00
try , LastActiveTab := driver . URL
2023-02-28 18:40:06 -05:00
; / loop through tabs
}
Message = ActivateChromeTab function failed to activate tab for %urlBase% after looping through %NumberOfTabActivationLoops% tabs
2023-08-20 00:36:38 -04:00
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile " )
2023-02-28 18:40:06 -05:00
return " Failed "
}
CheckForAlerts ( ) {
Message = Checking for Any Obstructing Alerts in Chrome
2023-08-20 00:36:38 -04:00
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile " )
2023-02-28 18:40:06 -05:00
status := 1
try driver . SwitchToAlert ( )
catch e {
status := 0
return 0
}
if ( status ) {
; msgbox, alert found
; Message = Page Alert Found. Dismissing.
2023-08-20 00:36:38 -04:00
Message = Page Alert Found . Accepting so page gets refreshed .
2023-02-28 18:40:06 -05:00
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
try driver . switchToalert ( ) . accept ( )
; try driver.switchToalert().dismiss()
return " True "
}
}
CheckURLForSubstring ( Substring ) {
try CurrentTabURL := driver . url
if ( InStr ( CurrentTabURL , Substring ) )
return " True "
else ,
return
}
; End of Function
; -------------------------------/NavigateFromBaseURL-------------------------------
2023-07-24 22:30:42 -04:00
; -------------------------------SaveDriverURL-------------------------------
2023-02-28 18:40:06 -05:00
SaveDriverURL ( ) { ; save the url of the result page. That way if a tab is not found for a site, we can open up a tab from this tab instead of middle of nowhere. That way we can keep the tabs together
try LastWebsitePostURL := driver . URL
return LastWebsitePostURL
}
2023-07-28 19:55:55 -04:00
; -------------------------------ConnectToChrome-------------------------------
ConnectToChrome ( URL := " " , Profile := " Profile 1 " , IP_Port := " 127.0.0.1:9222 " ) {
2023-07-28 10:15:46 -04:00
Message = Trying to Connect to Chrome
SaveOrPostProgress ( Message := Message , PostType := " Tooltip " )
2023-02-28 18:40:06 -05:00
IP_Port_Nr := RegExReplace ( IP_Port , " .*:(\d*) " , " $1 " )
if WinExist ( " ahk_exe Chrome.exe " ) {
WinGet , pid , PID , ahk_exe chrome . exe
for item in ComObjGet ( " winmgmts: " ) . ExecQuery ( " SELECT * FROM Win32_Process WHERE ProcessId=' " pid " ' " ) {
if RegExMatch ( item . CommandLine , " i)--remote-debugging-port=\K\d+ " , port ) {
break
}
}
if ( Port = " " ) {
MsgBox , 36 , , Chrome Needs to be started in debugging mode in order for the Uploader to control it with Selenium . `n Automatically restart Chrome in debug mode to enable a connection ?
IfMsgBox , Yes
{
Message = Restarting Chrome in Debug Mode
SaveOrPostProgress ( Message := Message , PostType := " Tooltip,ErrorLoggingTextFile,DiscordErrorLogging " )
While ( WinExist ( " ahk_exe chrome.exe " ) ) {
WinClose , ahk_exe chrome . exe
}
Process , WaitClose , chrome . exe
}
Else {
Msgbox , 4096 , Error , Cannot connect to Chrome profile if it is Not running in debug mode . Program Exiting
ExitApp
; @todo: Make this error out to the script result screen
}
}
}
2023-07-28 19:55:55 -04:00
2023-07-28 10:15:46 -04:00
if ( ChromeFilepath = " " ) {
2023-09-20 20:45:12 -04:00
GetInstalledChromeFilepath ( )
2023-07-28 10:15:46 -04:00
}
2023-02-28 18:40:06 -05:00
2023-07-28 10:15:46 -04:00
if ( ! winExist ( " ahk_exe chrome.exe " ) ) {
run , %ChromeFilepath% - - remote - debugging - port = %IP_Port_Nr% %URL%
}
2023-02-28 18:40:06 -05:00
2023-07-28 10:15:46 -04:00
Driver := ComObjCreate ( " Selenium.ChromeDriver " )
Driver . SetCapability ( " debuggerAddress " , IP_Port )
try Driver . Start ( )
catch e {
ShowSeleniumErrorMsgbox ( )
} ; end of catch
2023-02-28 18:40:06 -05:00
2023-07-28 10:15:46 -04:00
; Save current chrome version to ini file
return Driver
}
2023-02-28 20:42:12 -05:00
2023-07-28 19:55:55 -04:00
; -------------------------------/ConnectToChrome-------------------------------
2023-02-28 18:40:06 -05:00
/*
* /
; Various functions used to control Selenium, Chrome and Chrome.AHK
;---Javascript---
;------------------------------------------------
JS_TryToExecute ( JsToExecute , NumberofAttempts := 1 , SleepLength := 1000 ) {
loop , %NumberofAttempts% {
try driver . executeScript ( JsToExecute )
catch e {
Continue
}
Return
}
Return " Failed "
}
js_SendAndCheckWithQuerySelector ( Selector , ValueToCheck := " value " , SleepLength := 1000 , JSStringText := " TEXT " ) {
js = document . querySelector ( " %Selector% " ) . value = " %JSStringText% " ;
try driver . executeScript ( js )
sleep , %SleepLength%
js = return document . querySelector ( " #title " ) . value ;
try , status := driver . executeScript ( js )
; DevModeMsgBox(status)
if ( Status = " " )
return " Failed "
else ,
return " "
}
; ValueToCheckOptions = innertext,textContent,InnerHTML,outerHTML,value,href,option value
js_SendAndCheckWithClassName ( ClassName := " " , ClassIndexNum := 0 , ValueToCheck := " textContent " , SleepLength := 1000 , JSStringText := " TEXT " ) {
jsSend = document . getElementsByClassName ( '%ClassName% ') [ %ClassIndexNum% ] . value = " %JSStringText% " ;
try driver . executeScript ( jsSend )
sleep , %SleepLength%
jsCheck = return document . getElementsByClassName ( '%ClassName% ') [ %ClassIndexNum% ] . %ValueToCheck% ;
try Status := driver . executeScript ( jsCheck )
; Msgbox % "Status: " Status
if ( Status = " " )
return " Failed "
else ,
return " "
}
; ValueToCheckOptions = innertext,textContent,InnerHTML,outerHTML,value,href,option value
js_SendAndCheckWithNAME ( Element := " " , IndexNum := 0 , ValueToCheck := " textContent " , SleepLength := 1000 , JSStringText := " " ) {
jsSend = document . getElementsByName ( '%Element% ') [ %IndexNum% ] . value = " %JSStringText% " ;
/* Clipboard := jsSend
Msgbox % " jsSend: " jsSend
* /
; document.getElementsByClassName('%ClassName%')[%ClassIndexNum%].value = "%JSStringText%";
try driver . executeScript ( jsSend )
sleep , %SleepLength%
jsCheck = return document . getElementsByName ( '%Element% ') [ %IndexNum% ] . %ValueToCheck% ;
; Clipboard := jscheck
; Msgbox % "jsCheck: " jsCheck
try Status := driver . executeScript ( jsCheck )
; Msgbox % "Status: " Status
if ( Status = " " )
return " Failed "
else ,
return " "
}
; ValueToCheckOptions = innertext,textContent,InnerHTML,outerHTML,value,href,option value
js_SendAndCheckWithID ( Element := " " , ValueToCheck := " textContent " , SleepLength := 1000 , JSStringText := " " ) {
; Msgbox % "Element: " Element
; Msgbox % "JSStringText: " JSStringText
jsSend = document . getElementById ( '%Element% ') . value = " %JSStringText% " ;
; Clipboard := jsSend
; Msgbox % "jsSend: " jsSend
try driver . executeScript ( jsSend )
; Msgbox % "JSStringText: " JSStringText
sleep , %SleepLength%
jsCheck = return document . getElementById ( '%Element% ') . %ValueToCheck% ;
try Status := driver . executeScript ( jsCheck )
if ( Status = " " )
return " Failed "
else ,
return " "
}
;---\Javascript---
;------------------------------------------------
;---Selenium---
;------------------------------------------------
; When called these will try multiple times to click/input into a web element
Selenium_LoopToClickID ( IDName , NumOfLoops := 1 , SleepLength := 1000 ) {
loop , %NumOfLoops% {
try driver . findElementsByID ( IDName ) . item [ 1 ] . click ( ) ; Click on "upload image" button
catch e {
if ( A_index = NumOfLoops ) {
Return " Failed "
}
sleep , %SleepLength%
Continue
}
2023-09-17 14:25:54 -04:00
MouseClicksSaved + = 1
2023-02-28 18:40:06 -05:00
Return
}
}
Selenium_LoopToClickName ( ElementName , NumOfLoops := 1 , SleepLength := 1000 ) {
loop , %NumOfLoops% {
try driver . findElementsByName ( ElementName ) . item [ 1 ] . click ( )
catch e {
if ( A_index = NumOfLoops ) {
Return " Failed "
}
sleep , %SleepLength%
Continue
}
2023-09-17 14:25:54 -04:00
MouseClicksSaved + = 1
2023-02-28 18:40:06 -05:00
Return
}
}
Selenium_LoopToSendValueToID ( IDName , NumOfLoops := 1 , SleepLength := 1000 , StringTextContent := " " ) {
loop , %NumOfLoops% {
try driver . findElementsByID ( IDName ) . item [ 1 ] . sendKeys ( StringTextContent ) ; Click on "upload image" button
catch e {
if ( A_index = NumOfLoops ) {
Return " Failed "
}
sleep , %SleepLength%
Continue
}
Return
}
}
; Selenium_LoopToSendValueByName(ElementName:="NAME",NumOfLoops:=2,SleepLength:=1000,StringTextContent:="TEXT")
Selenium_LoopToSendValueByName ( ElementName , NumOfLoops := 1 , SleepLength := 1000 , StringTextContent := " " ) {
loop , %NumOfLoops% {
try driver . findElementsByName ( ElementName ) . item [ 1 ] . SendKeys ( StringTextContent )
catch e {
if ( A_index = NumOfLoops ) {
Return " Failed "
}
sleep , %SleepLength%
Continue
}
Return
}
}
Selenium_LoopToSendValueToXpath ( Xpath , NumOfLoops := 1 , SleepLength := 1000 , StringTextContent := " " ) {
loop , %NumOfLoops% {
; ToolTip, Loop attempt: %A_index%
try driver . FindElementByXPath ( Xpath ) . sendKeys ( StringTextContent ) ; Click on "upload image" button
catch e {
if ( A_index = NumOfLoops ) {
Return " Failed "
}
sleep , %SleepLength%
Continue
}
Return
}
}
Selenium_LoopToClickXpath ( Xpath , NumOfLoops := 1 , SleepLength := 1000 ) {
loop , %NumOfLoops% {
try driver . FindElementByXPath ( Xpath ) . click ( )
catch e {
if ( A_index = NumOfLoops ) {
Return " Failed "
}
sleep , %SleepLength%
Continue
}
2023-09-17 14:25:54 -04:00
MouseClicksSaved + = 1
Return
2023-02-28 18:40:06 -05:00
}
}
Selenium_LoopToClickXpathAndWaitForOpenWindow ( Xpath , NumOfLoops := 1 , SleepLength := 1000 , WindowName := " Open " ) {
loop , %NumOfLoops% {
; TooltipThis("Clicking xpath")
try driver . FindElementByXPath ( Xpath ) . click ( )
catch e {
if ( A_index = NumOfLoops ) {
Return " Failed to Click Xpath or Open File window did not show up on click "
}
sleep , %SleepLength%
Continue
}
; tooltipthis("Checking if window exists")
sleep , 1000
; Msgbox % "WindowName: " WindowName
if ( ! WinExist ( WindowName ) ) {
Message = %WindowName% not found on %A_index% attempt .
; tooltipthis("Window not found")
sleep , %SleepLength%
Continue
}
2023-09-17 14:25:54 -04:00
MouseClicksSaved + = 1
Return
2023-02-28 18:40:06 -05:00
}
}
Selenium_LoopToClearXpath ( Xpath , NumOfLoops := 1 , SleepLength := 1000 ) {
loop , %NumOfLoops% {
try driver . FindElementByXPath ( Xpath ) . clear ( )
catch e {
if ( A_index = NumOfLoops ) {
Return " Failed "
}
sleep , %SleepLength%
Continue
}
Return
}
}
2023-07-30 22:14:41 -04:00
Selenium_TypeTextIntoElement ( Text , Xpath ) {
StrLengthOfPost := StrLen ( Text )
Loop % StrLengthOfPost {
Current_Char := SubStr ( Text , A_index , 1 )
; Msgbox % "Current_Char: " Current_Char
; driver.FindElementByXPath(Xpath).SendKeys(VAR)
Status := Selenium_LoopToSendValueToXpath ( Xpath := Xpath , NumOfLoops := 1 , SleepLength := 100 , StringTextContent := Current_Char )
if ( Status ) {
Message = Failed to Input Text
Return Message
}
}
}
2023-07-24 22:30:42 -04:00
SaveDriverURLOFErrorPage ( ) { ; save the url of the result page. That way if a tab is not found for a site, we can open up a tab from this tab instead of middle of nowhere. That way we can keep the tabs together
try URLOfLastErrorPage := driver . URL
}
2023-02-28 18:40:06 -05:00
;---\Selenium---
;------------------------------------------------
; -------------------------------Javascript-------------------------------
ReturnAndDisplayJSData ( jsref ) {
; msgbox, here goes
; https://www.w3schools.com/jsref/dom_obj_all.asp
; -----TEXT CONTENT-----
js = return %jsref% . textContent ;
try status := driver . executeScript ( js )
OnMessage ( 0x44 , " OnMsgBoxJSReturnData " )
MsgBox 0x3 , . TextContent : , %status%
OnMessage ( 0x44 , " " )
/*
* /
IfMsgBox Yes , {
} Else IfMsgBox No , {
TextForClip = js = %js% `n try , status := driver . executeScript ( js )
Clipboard := TextForClip
} Else IfMsgBox Cancel , {
return
}
; -----VALUE-----
js = return %jsref% . value ;
try status := driver . executeScript ( js )
OnMessage ( 0x44 , " OnMsgBoxJSReturnData " )
MsgBox 0x3 , . value : , %status%
OnMessage ( 0x44 , " " )
/*
* /
IfMsgBox Yes , {
} Else IfMsgBox No , {
TextForClip = js = %js% `n try , status := driver . executeScript ( js )
Clipboard := TextForClip
} Else IfMsgBox Cancel , {
return
}
; -----INNERTEXT-----
js = return %jsref% . innerText ;
try status := driver . executeScript ( js )
OnMessage ( 0x44 , " OnMsgBoxJSReturnData " )
MsgBox 0x3 , . innerText , %status%
OnMessage ( 0x44 , " " )
/*
* /
IfMsgBox Yes , {
} Else IfMsgBox No , {
TextForClip = js = %js% `n try , status := driver . executeScript ( js )
Clipboard := TextForClip
} Else IfMsgBox Cancel , {
return
}
; -----OuterText-----
js = return %jsref% . outerText ;
try status := driver . executeScript ( js )
OnMessage ( 0x44 , " OnMsgBoxJSReturnData " )
MsgBox 0x3 , . outerText , %status%
OnMessage ( 0x44 , " " )
/*
* /
IfMsgBox Yes , {
} Else IfMsgBox No , {
TextForClip = js = %js% `n try , status := driver . executeScript ( js )
Clipboard := TextForClip
} Else IfMsgBox Cancel , {
return
}
;-----innerHTML-----
js = return %jsref% . innerHTML ;
try status := driver . executeScript ( js )
OnMessage ( 0x44 , " OnMsgBoxJSReturnData " )
MsgBox 0x3 , . innerHTML , %status%
OnMessage ( 0x44 , " " )
/*
* /
IfMsgBox Yes , {
} Else IfMsgBox No , {
TextForClip = js = %js% `n try , status := driver . executeScript ( js )
Clipboard := TextForClip
} Else IfMsgBox Cancel , {
return
}
; -----outerHTML-----
js = return %jsref% . outerHTML ;
try status := driver . executeScript ( js )
OnMessage ( 0x44 , " OnMsgBoxJSReturnData " )
MsgBox 0x3 , . outerHTML , %status%
OnMessage ( 0x44 , " " )
/*
* /
IfMsgBox Yes , {
} Else IfMsgBox No , {
TextForClip = js = %js% `n try , status := driver . executeScript ( js )
Clipboard := TextForClip
} Else IfMsgBox Cancel , {
return
}
}
/*
* /
OnMsgBoxJSReturnData ( ) {
DetectHiddenWindows , On
Process , Exist
If ( WinExist ( " ahk_class #32770 ahk_pid " . ErrorLevel ) ) {
ControlSetText Button1 , OK
ControlSetText Button2 , Copy
ControlSetText Button3 , Return
}
}
2023-07-24 22:30:42 -04:00