Re-worked and cleaned up Chrome Tab indexing and selecting

pull/2/head
Yuriy 2 years ago
parent e7d4b140e0
commit 232e3baf6c

@ -4,12 +4,13 @@
; -------------------------------Variables-------------------------------
; Declare global variables here so they don't have to be declared in each script
global DriverURLArray
global ChromeTabsURLArray
global DriverTitleArray
global LastWebsitePostURL
global ScreenshotsTaken
global ChromeFilepath
global CurrentTabURL
global URLOfLastErrorPage
; #Include %A_ScriptDir%\ChromeAutomationFunctions.ahk
; #Include %A_ScriptDir%\RunCMD.ahk
@ -40,10 +41,10 @@ CheckForUpdates(GitReleasesAPIURL){
if(ScriptVersion = UpdateVersionNumber OR ScriptVersion > UpdateVersionNumber){
ToolTip
return
}
else, {
ToolTip
return
}
else, {
UpdateAvailable := 1
; msgbox, update found!
Message = Program Update Found
@ -127,6 +128,9 @@ SaveOrPostProgress(Message:="",PostType:=""){
if(InStr(PostType, "DiscordErrorLogging")){
PostToDiscordChannel(Message,DiscordErrorLoggingWebhookBotURL)
}
if(InStr(PostType, "DevModeMsgBox")){
DevModeMsgBox(Message)
}
if(InStr(PostType, "DiscordVideos")){
Message := MessageBU
PostToDiscordChannel(Message,DiscordVideosWebhookURL)
@ -149,11 +153,11 @@ TakeScreenshotOfPage(SaveFilepath := ""){
; sleep, 5000
if(SaveFilepath = ""){
SaveFilepath := ErrorLoggingDirectory
}
SaveFilepath := ErrorLoggingDirectory
}
; if(!FileExist(SaveFilepath))
FileCreateDir, %SaveFilepath%
; if(!FileExist(SaveFilepath))
FileCreateDir, %SaveFilepath%
/* if(!TakeScreenshotsOfErrors)
Return
@ -766,6 +770,11 @@ CheckSeleniumDriver(){
Return "Failed"
}
DriverStatus := 1
if(ReuseTabs)
CreateArrayOfChromeTabs()
}
SeleniumConnectToActiveTab(IP_Port := "127.0.0.1:9222"){
@ -781,15 +790,17 @@ SeleniumConnectToActiveTab(IP_Port := "127.0.0.1:9222"){
return Driver
}
CreateArrayOfTabs() {
CreateArrayOfChromeTabs() {
Message = Creating an Array of All Chrome Tabs
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
; Create an arrays
DriverTitleArray := []
DriverURLArray := []
ChromeTabsURLArray := []
try TotalTabsFound := Driver.Windows.Count
; Message = TotalTabsFound: %TotalTabsFound%
; DevModeMsgBox(Message)
; DevModeMsgBox(TotalTabsFound)
/* ; if only 1 tab exists, grab info, push to array and return
@ -800,7 +811,7 @@ CreateArrayOfTabs() {
if(Title != "")
DriverTitleArray.Push(Title)
if(URL != "")
DriverURLArray.Push(URL)
ChromeTabsURLArray.Push(URL)
return
}
@ -820,14 +831,21 @@ CreateArrayOfTabs() {
; 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
TotalLoops := TotalTabsFound + 5
Loop % TotalLoops {
Message = Creating an Array of All Chrome Tabs`nCurrent Loop: %A_index%/%TotalLoops% (Cannot check on unloaded tabs)`nSometimes might get stuck for a short while if there is something loading in active tab
TotalTabLoops := TotalTabsFound + 5
; Message = TotalTabLoops: %TotalTabLoops%
; DevModeMsgBox(message)
Loop % TotalTabLoops {
Message = Creating an Array of All Chrome Tabs`nCurrent Loop: %A_index%/%TotalTabLoops% (Cannot check on unloaded tabs)`nSometimes might get stuck for a short while if there is something loading in active tab
TooltipThis(Message)
; exit after looping through 1 tab, if only 1 tab found in count
if(TotalTabsFound = 1 AND A_index > TotalTabsFound)
return
if(TotalTabsFound = 1 AND A_index > TotalTabsFound){
Message = Exiting Early after 1 loop
DevModeMsgBox(message)
}
TabEndTime := A_TickCount - TabFoundStartTime
TabEndTimeArray .= TabEndTime . ","
@ -837,9 +855,7 @@ CreateArrayOfTabs() {
; SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
TabFoundStartTime := A_TickCount
if(TotalTabsFound < 4 AND A_index = 4)
break
; TimeToGrabCurrentTab := A_TickCount - StartTime
; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab
@ -851,140 +867,196 @@ CreateArrayOfTabs() {
; TimeToGrabCurrentTab := A_TickCount - StartTime
; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab
; VAR := HasVal(DriverURLArray, STRINGVAR) ; returns position of value in array
; VAR := HasVal(ChromeTabsURLArray, STRINGVAR) ; returns position of value in array
/*
if(HasVal(ChromeTabsURLArray, URL)){ ; returns position of value in array
DevModeMsgBox("Skipping rest of loop and continuing")
Continue
}
if(HasVal(DriverURLArray, URL)){ ; returns position of value in array
Continue
}
*/
if(Title != "")
DriverTitleArray.Push(Title)
if(URL != "")
ChromeTabsURLArray.Push(URL)
if(Title != "")
DriverTitleArray.Push(Title)
if(URL != "")
DriverURLArray.Push(URL)
try driver.SwitchToNextWindow ;Focuses Selenium on the newly opened/next window.
; DevModeMsgBox("switching to next tab")
} ; end of loop
try driver.SwitchToNextWindow ;Focuses Selenium on the newly opened/next window.
} ; end of loop
; TimeToLoopThroughAllTabs := A_TickCount - StartTime
; Message = Time to Loop through all tabs: %TimeToLoopThroughAllTabs%
; SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
; DevModeMsgBox(Message)
; TimeToLoopThroughAllTabs := A_TickCount - StartTime
; Message = Time to Loop through all tabs: %TimeToLoopThroughAllTabs%
; SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
; DevModeMsgBox(Message)
return
} ; /FUNC
return
InputFilePathIntoOpenWindow(Filepath){
; Msgbox % "Filepath: " Filepath
Message = Waiting for "Open" window to appear to input filepath into
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
; Tooltip,Waiting for "Open" window to appear to input filepath into,850,0
; WinWait, Open
WinWait,Open,,5 ; Wait for 10 seconds for window
if(ErrorLevel)
{
; msgbox, failed to find window.
Return "Failed"
}
WinActivate, Open
sleep, 1000
ControlSetText, Edit1, %Filepath%, Open
sleep, 1000
ControlSend, Edit1, {Enter}, Open
sleep, 1000
; Do an extra check in case the Open window is still open.
OpenWindowExist := WinExist("Open")
if(OpenWindowExist)
ControlSend, Edit1, {Enter}, Open
ToolTip,
}
InputFilePathIntoOpenWindow(Filepath){
; Msgbox % "Filepath: " Filepath
Message = Waiting for "Open" window to appear to input filepath into
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
; Tooltip,Waiting for "Open" window to appear to input filepath into,850,0
; WinWait, Open
WinWait,Open,,5 ; Wait for 10 seconds for window
if(ErrorLevel)
{
; msgbox, failed to find window.
Return "Failed"
}
WinActivate, Open
sleep, 1000
ControlSetText, Edit1, %Filepath%, Open
sleep, 1000
ControlSend, Edit1, {Enter}, Open
sleep, 1000
; Do an extra check in case the Open window is still open.
OpenWindowExist := WinExist("Open")
if(OpenWindowExist)
ControlSend, Edit1, {Enter}, Open
ToolTip,
}
; -------------------------------NavigateFromBaseURLTo-------------------------------
NavigateFromBaseURLTo(URL,PageTitle := "TodoDeleteme"){
if(!DriverStatus){ ; if not connected to selenium chrome, then re-connect
Status := CheckSeleniumDriver()
if(Status)
Return "Failed"
; -------------------------------NavigateFromBaseURLTo-------------------------------
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]
CreateArrayOfTabs()
}
return URLBase
}
; Msgbox % CreateArrayOfTabs.Length() ; Display total number of items in the array
ArrayLenght := DriverURLArray.Length() ; Save total number of items in the array
; Msgbox % "ArrayLenght: " ArrayLenght
; Iterate from 1 to the end of the array:
/*
Loop % DriverURLArray.Length(){
; ArrayItem := CreateArrayOfTabs[A_Index]
MsgBox % DriverURLArray[A_Index]
}
ActivateChromeTab(URL){
; convert to baseUR
if(InStr(URL, "/")){
URLBase := ExtractBaseURL(URL)
}
else, {
URLBase := URL
}
*/
; message = URL: %URL%
; DevModeMsgBox(message)
Message = Finding Tab with URL base: %URLBase%
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
DevModeMsgBox(Message)
if(ReuseTabs){
URLBase := StrReplace(URL,"https://","") ; remove beginning of URL
URLBase := StrSplit(URLBase, "/") ; remove text after url base
URLBase := URLBase[1]
URLBase := StrReplace(URLBase, "www.","") ; remove www if there 1
}
; Message = URLbase after cleaning: %URLBase%`nOriginal URL: %URL%
; DevModeMsgBox(message)
; if not connected to selenium chrome, then re-connect
if(!DriverStatus){
Status := CheckSeleniumDriver()
if(Status)
Return "Failed"
; check if URLBase is a substring within any of the values in the array
if(!HasSubstringVal(DriverURLArray, URLBase))
{
; DevModeMsgBox("Creating new tab")
try, run "%ChromeFilepath%" "%URL%"
DriverURLArray.Push(URL) ; Append the new url to the array
NewTabCreated := 1
sleep, 1000
}
/*
else, {
Message = %URLBase% is the urlbase and it's within the DriverURLArray
DevModeMsgBox(Message)
}
*/
; msgbox,
; CreateArrayOfChromeTabs() ; moved to the end of checkseleniumdriver()
}
Message = Finding Tab
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
DevModeMsgBox(Message)
TabFoundSuccessfully :=
TabFoundSuccessfully :=
NumberOfTabLoops := DriverURLArray.Length()
NumberOfTabActivationLoops := ChromeTabsURLArray.Length()
Message := "NumberOfTabActivationLoops: " NumberOfTabActivationLoops
DevModeMsgBox(Message)
; NumberOfTabLoops := Driver.Windows.Count + 2
loop % NumberOfTabLoops { ; loop through tabs to find matching tab
try driver.SwitchToNextWindow()
; NumberOfTabLoops := Driver.Windows.Count + 2
loop % NumberOfTabActivationLoops { ; loop through tabs to find matching tab
try driver.SwitchToNextWindow()
; sleep, 1000
; msgbox, looping through tabs
try CurrentTabTitle := driver.window.title
try CurrentTabURL := driver.Url
; sleep, 1000
; msgbox, looping through tabs
try CurrentTabTitle := driver.window.title
try CurrentTabURL := driver.Url
if(CurrentTabTitle = PageTitle OR InStr(CurrentTabURL, URLBase)){
if(InStr(CurrentTabURL, URLBase)){
Message = Found Tab:.`n%CurrentTabTitle% = %PageTitle%`n%CurrentTabURL% = %URLBase%
DevModeMsgBox(message)
Message = Found Tab:.`n%CurrentTabTitle% = %PageTitle%`n%CurrentTabURL% = %URLBase%
DevModeMsgBox(message)
TabFoundSuccessfully := 1
; msgbox %CurrentTabTitle% = %PageTitle%
; msgbox %CurrentTabURL% = %URL%
Break
}
TabFoundSuccessfully := 1
; msgbox %CurrentTabTitle% = %PageTitle%
; msgbox %CurrentTabURL% = %URL%
return
}
; / loop through tabs
}
return "Failed"
}
; / loop through tabs
/*ActivateChromeTab(URL){
; ChromeTabsURLArray
if(!HasSubstringVal(ChromeTabsURLArray, URLBase))
{
try, run "%ChromeFilepath%" "%URL%"
ChromeTabsURLArray.Push(URL) ; Append the new url to the array
NewTabCreated := 1
sleep, 1000
}
}
*/
NavigateFromBaseURLTo(URL,PageTitle := "TodoDeleteme"){
if(!DriverStatus){ ; if not connected to selenium chrome, then re-connect
Status := CheckSeleniumDriver()
if(Status)
Return "Failed"
; CreateArrayOfChromeTabs() ; moved to the end of checkseleniumdriver()
}
; ArrayOfOpenTabsLength := ChromeTabsURLArray.Length() ; Save total number of items in the array
; message = URL: %URL%
; DevModeMsgBox(message)
URLBase := ExtractBaseURL(URL)
/* if(ReuseTabs){
URLBase := StrReplace(URL,"https://","") ; remove beginning of URL
URLBase := StrSplit(URLBase, "/") ; remove text after url base
URLBase := URLBase[1]
URLBase := StrReplace(URLBase, "www.","") ; remove www if there 1
}
*/
; Message = URLbase after cleaning: %URLBase%`nOriginal URL: %URL%
; DevModeMsgBox(message)
; if URLBase is NOT a substring within any of the values in the array
if(!HasSubstringVal(ChromeTabsURLArray, URLBase))
{
; DevModeMsgBox("Creating new tab")
try, run "%ChromeFilepath%" "%URL%"
ChromeTabsURLArray.Push(URL) ; Append the new url to the array
NewTabCreated := 1
sleep, 1000
}
message = NewTabCreated: %NewTabCreated%
DevModeMsgBox(message)
status := ActivateChromeTab(URLBase)
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
@ -1018,6 +1090,10 @@ CreateArrayOfTabs() {
Return
}
; End of Function
; -------------------------------/NavigateFromBaseURL-------------------------------
@ -1040,7 +1116,7 @@ SChrome_Get(URL := "", Profile := "Profile 1", IP_Port := "127.0.0.1:9222"){
}
if (Port=""){
MsgBox, 36, ,Chrome Needs to be started in debugging mode in order for Autohotkey to connect to it.`nIs it ok to restart Chrome in debugmode to enable a connection?
MsgBox, 36, ,Chrome Needs to be started in debugging mode in order for the Uploader to control it with Selenium.`nAutomatically restart Chrome in debug mode to enable a connection?
IfMsgBox, Yes
{
Message = Restarting Chrome in Debug Mode
@ -1052,7 +1128,7 @@ SChrome_Get(URL := "", Profile := "Profile 1", IP_Port := "127.0.0.1:9222"){
Process, WaitClose, chrome.exe
}
Else{
Msgbox, 4096, Error, Cannot connect to Chrome profile if it is Not running in debug mode. Script Terminating
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
}
@ -1404,22 +1480,3 @@ SaveDriverURLOFErrorPage(){ ; save the url of the result page. That way if a tab
try URLOfLastErrorPage := driver.URL
}
FindAndActivateTab(TabURL){
Loop, {
if(A_index = 30)
break
try CurrentTabTitle := driver.window.title
try CurrentTabURL := driver.Url
if(CurrentTabURL = TabURL)
Break
try driver.SwitchToNextWindow()
}
}

Loading…
Cancel
Save