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
@ -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)
@ -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 . ","
@ -838,8 +856,6 @@ CreateArrayOfTabs() {
TabFoundStartTime := A_TickCount
if(TotalTabsFound < 4 AND A_index = 4)
break
; TimeToGrabCurrentTab := A_TickCount - StartTime
; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab
@ -851,30 +867,36 @@ 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(DriverURLArray, URL)){ ; 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(Title != "")
DriverTitleArray.Push(Title)
if(URL != "")
DriverURLArray.Push(URL)
ChromeTabsURLArray.Push(URL)
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
} ; /FUNC
}
InputFilePathIntoOpenWindow(Filepath){
; Msgbox % "Filepath: " Filepath
@ -903,68 +925,50 @@ CreateArrayOfTabs() {
}
; -------------------------------NavigateFromBaseURLTo-------------------------------
NavigateFromBaseURLTo(URL,PageTitle := "TodoDeleteme"){
if(!DriverStatus){ ; if not connected to selenium chrome, then re-connect
Status := CheckSeleniumDriver()
if(Status)
Return "Failed"
CreateArrayOfTabs()
}
; 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]
}
*/
; message = URL: %URL%
; DevModeMsgBox(message)
if(ReuseTabs){
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]
URLBase := StrReplace(URLBase, "www.","") ; remove www if there 1
return URLBase
}
; Message = URLbase after cleaning: %URLBase%`nOriginal URL: %URL%
; DevModeMsgBox(message)
; 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
ActivateChromeTab(URL){
; convert to baseUR
if(InStr(URL, "/")){
URLBase := ExtractBaseURL(URL)
}
/*
else, {
Message = %URLBase% is the urlbase and it's within the DriverURLArray
DevModeMsgBox(Message)
URLBase := URL
}
*/
; msgbox,
Message = Finding Tab
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"
; CreateArrayOfChromeTabs() ; moved to the end of checkseleniumdriver()
}
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
loop % NumberOfTabActivationLoops { ; loop through tabs to find matching tab
try driver.SwitchToNextWindow()
; sleep, 1000
@ -972,7 +976,7 @@ CreateArrayOfTabs() {
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)
@ -980,11 +984,79 @@ CreateArrayOfTabs() {
TabFoundSuccessfully := 1
; msgbox %CurrentTabTitle% = %PageTitle%
; msgbox %CurrentTabURL% = %URL%
Break
return
}
; / loop through tabs
}
return "Failed"
}
/*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