You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
929 lines
26 KiB
Plaintext
929 lines
26 KiB
Plaintext
1 year ago
|
; Notes/Extra Info/#Includes
|
||
2 years ago
|
;------------------------------------------------
|
||
1 year ago
|
; Selenium Related Functions
|
||
2 years ago
|
|
||
2 years ago
|
|
||
1 year ago
|
; Global Variables
|
||
|
;------------------------------------------------
|
||
|
; Declare global variables here so they don't have to be declared in each script
|
||
|
global ChromeTabsURLArray
|
||
|
global DriverTitleArray
|
||
|
global LastWebsitePostURL
|
||
|
global CurrentTabURL
|
||
|
global URLOfLastErrorPage
|
||
2 years ago
|
|
||
|
ShowSeleniumErrorMsgbox(){
|
||
|
|
||
|
; Clipboard := ChromeVersion
|
||
|
; Msgbox % "Chromeversion: " Chromeversion
|
||
|
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")
|
||
|
MsgBox 0x40043, Error, Failed to Connect to Chrome. `nMost likely issue is either Chrome has some sort of dialogue box open or ChromeDriver needs to be updated.`n`nClick "Reload" to reload the script to try again`nClick "ChromeDriver" to open up the ChromeDriver download page. `n`nClick "Instructions" to open up the instructions website for updating Chromedriver.`n`nPossibly Helpful Info:`nCurrent Chrome Version: %Chromeversion%Chrome Version of Last Successfull Upload: %PreviousWorkingChromeVersion%
|
||
|
OnMessage(0x44, "")
|
||
|
|
||
|
IfMsgBox Yes, {
|
||
|
; Reload script
|
||
|
Reload
|
||
|
} Else IfMsgBox No, {
|
||
|
run, https://chromedriver.chromium.org/downloads
|
||
|
ExitApp
|
||
|
; open chromedriver site
|
||
|
} Else IfMsgBox Cancel, {
|
||
|
URL = https://git.freedomainplaylists.com/yuriy/Freedomain-Social-Media-Poster#installation
|
||
|
run, %url%
|
||
|
}
|
||
|
ExitApp
|
||
|
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
*/
|
||
|
|
||
|
|
||
2 years ago
|
; -------------------------------Tab Navigation & Activation-------------------------------
|
||
2 years ago
|
|
||
|
CheckSeleniumDriver(){
|
||
|
try Driver := SChrome_Get("", ChromeProfile := "") ; open new tab page with with specified profile
|
||
|
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"
|
||
|
}
|
||
|
; Driver := SChrome_Get("https://www.bitchute.com/", ChromeProfile) ; open new tab page with with specified profile
|
||
|
; 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
|
||
|
|
||
|
if(ReuseTabs)
|
||
|
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
|
||
|
|
||
2 years ago
|
StartTime := A_TickCount
|
||
2 years ago
|
|
||
|
|
||
|
|
||
2 years ago
|
; 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
|
||
2 years ago
|
TotalTabLoops := TotalTabsFound + 2
|
||
2 years ago
|
|
||
2 years ago
|
Message = Total Pre-Existing Tabs in Chrome: %TotalTabsFound%`nTotal Times looping through tabs to create Chrome Tabs Array: %TotalTabLoops%
|
||
|
SaveOrPostProgress(Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
|
||
2 years ago
|
|
||
|
|
||
|
|
||
2 years ago
|
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)
|
||
2 years ago
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
Loop % TotalTabLoops {
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
try Title := Driver.Title
|
||
|
try URL := Driver.URL
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
if(Title != "")
|
||
|
DriverTitleArray.Push(Title)
|
||
|
if(URL != "")
|
||
|
ChromeTabsURLArray.Push(URL)
|
||
2 years ago
|
|
||
2 years ago
|
; 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
|
||
|
}
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
TabEndTime := A_TickCount - TabFoundStartTime
|
||
|
TabEndTimeArray .= TabEndTime . ","
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
; Message = Time to switch to new tab: %TabEndTime%`nCurrent Loop: %A_index%/%TotalTabsFound%
|
||
|
; SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
|
||
2 years ago
|
|
||
2 years ago
|
TabFoundStartTime := A_TickCount
|
||
2 years ago
|
|
||
2 years ago
|
; TimeToGrabCurrentTab := A_TickCount - StartTime
|
||
|
; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab
|
||
2 years ago
|
|
||
2 years ago
|
; Stop page refresh if it's happening
|
||
|
; try driver.executeScript("return window.stop")
|
||
2 years ago
|
|
||
|
|
||
|
|
||
2 years ago
|
; TimeToGrabCurrentTab := A_TickCount - StartTime
|
||
|
; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab
|
||
|
; VAR := HasVal(ChromeTabsURLArray, STRINGVAR) ; returns position of value in array
|
||
2 years ago
|
|
||
|
/*
|
||
|
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
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
*/
|
||
|
|
||
2 years ago
|
CheckCurrentTabForCurrentSite(){
|
||
2 years ago
|
try CurrentTabURL := GetCurrentTabURlBase()
|
||
2 years ago
|
|
||
2 years ago
|
CheckForAlerts()
|
||
|
|
||
2 years ago
|
if(!InStr(CurrentTabURL, CurrentSite)){
|
||
2 years ago
|
Message = Chromedriver failed to switch to %CurrentSite%. Current Tab URL: %CurrentTabURL%
|
||
2 years ago
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
|
||
|
return "Failed"
|
||
|
}
|
||
2 years ago
|
else, {
|
||
|
Message = CheckCurrentTabForCurrentSite() passed successfully
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
}
|
||
2 years ago
|
}
|
||
|
/*
|
||
|
*/
|
||
|
|
||
|
|
||
2 years ago
|
GetCurrentTabURLBase(){
|
||
2 years ago
|
try, TabURL := driver.url
|
||
|
TabURL := ExtractBaseURL(TabURL)
|
||
|
return TabURL
|
||
|
}
|
||
|
|
||
2 years ago
|
|
||
|
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)
|
||
|
|
||
2 years ago
|
Message = Checking for URLBase: %URLBase% within ChromeTabsURLArray
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
|
||
2 years ago
|
; msgbox % HasSubstringVal(ChromeTabsURLArray, URLBase) . "`n`n is the status of URLbase in ChromeTabsURLArray"
|
||
2 years ago
|
|
||
|
; if URLBase is NOT a substring within any of the values in the array
|
||
|
if(!HasSubstringVal(ChromeTabsURLArray, URLBase))
|
||
|
{
|
||
|
; DevModeMsgBox("Creating new tab")
|
||
2 years ago
|
Message = Chrome tab does not exist. Creating new tab ;. Creating new tab with URL: %URL%
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
|
||
2 years ago
|
try, run "%ChromeFilepath%" "%URL%"
|
||
2 years ago
|
catch e {
|
||
|
Message = Trying to Run Chrome with URL failed for some reason. Wrong Chrome filepath?
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
}
|
||
2 years ago
|
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"){
|
||
2 years ago
|
try currentURL := GetCurrentTabURLBase()
|
||
2 years ago
|
Message = Failed to switch active tab to %URLBase%, currently on different site.
|
||
2 years ago
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
2 years ago
|
|
||
|
CheckForAlerts()
|
||
|
|
||
|
; create a new tab and try to switch to it again
|
||
2 years ago
|
Message = Creating new Chrome tab with URL: %URL%
|
||
2 years ago
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
|
||
|
try, run "%ChromeFilepath%" "%URL%"
|
||
2 years ago
|
catch e {
|
||
|
Message = Trying to Run Chrome with URL failed for some reason. Wrong Chrome filepath?
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
}
|
||
2 years ago
|
ChromeTabsURLArray.Push(URL) ; Append the new url to the array
|
||
|
NewTabCreated := 1
|
||
|
sleep, 1000
|
||
2 years ago
|
status := ActivateChromeTab(URL)
|
||
2 years ago
|
if(Status = "Failed"){
|
||
2 years ago
|
Message = Failed to activate tab for %urlBase% after 2 seperate attempts. Chrome stuck on a dialog box of some sort?
|
||
2 years ago
|
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`nPlease 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
|
||
|
}
|
||
|
|
||
|
|
||
2 years ago
|
; -------------------------------ActivateChromeTab-------------------------------
|
||
2 years ago
|
ActivateChromeTab(URL){
|
||
2 years ago
|
|
||
|
; Extract Base URL if necessary
|
||
2 years ago
|
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 :=
|
||
|
|
||
2 years ago
|
NumberOfTabActivationLoops := ChromeTabsURLArray.Length()
|
||
|
; Message = NumberOfTabActivationLoops has been increased by 3
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
2 years ago
|
|
||
|
Message = Looping through tabs -> %NumberOfTabActivationLoops% <- times to find tab with URL: %urlBase%
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
|
||
|
try, StartingTabURL := driver.url
|
||
|
|
||
2 years ago
|
; Message := "NumberOfTabActivationLoops: " NumberOfTabActivationLoops
|
||
|
; DevModeMsgBox(Message)
|
||
|
|
||
|
; NumberOfTabLoops := Driver.Windows.Count + 2
|
||
|
|
||
2 years ago
|
; Message = Looping through tabs to activate with: %URLBase%
|
||
|
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
|
||
|
; create empty variable for loop
|
||
|
LastActiveTab :=
|
||
2 years ago
|
|
||
|
loop % NumberOfTabActivationLoops { ; loop through tabs to find matching tab
|
||
|
|
||
2 years ago
|
UStartTime := A_TickCount ; start time
|
||
2 years ago
|
|
||
|
try driver.SwitchToNextWindow()
|
||
|
|
||
2 years ago
|
; 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
|
||
|
|
||
2 years ago
|
; sleep, 1000
|
||
|
; msgbox, looping through tabs
|
||
|
try CurrentTabTitle := driver.window.title
|
||
|
try CurrentTabURL := driver.Url
|
||
|
|
||
2 years ago
|
if(A_index < 3){
|
||
|
if(CurrentTabURL = StartingTabURL){
|
||
|
Message = Current Tab URL is THE SAME as Starting URL after %A_index% tab switches`nStuck on this tab?
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
CheckForAlerts()
|
||
|
}
|
||
|
else, {
|
||
|
Message = Current Tab URL is DIFFERENT from starting URL after %A_index% tab switches
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
}
|
||
|
|
||
|
if(LastActiveTab = CurrentTabURL){
|
||
|
Message = Chrome is on same tab URL after calling SwitchToNextWindow() function
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
CheckForAlerts()
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
2 years ago
|
if(InStr(CurrentTabURL, URLBase)){
|
||
|
|
||
2 years ago
|
TabURL := GetCurrentTabURLBase()
|
||
|
Message = Successfully Found and Activated Tab. URLBase: %urlBase% and currentTabURL: %TabURL%
|
||
|
|
||
2 years ago
|
; Message = Found Tab:.`n%CurrentTabTitle% = %PageTitle%`n%CurrentTabURL% = %URLBase%
|
||
|
; DevModeMsgBox(message)
|
||
|
|
||
|
TabFoundSuccessfully := 1
|
||
|
; msgbox %CurrentTabTitle% = %PageTitle%
|
||
|
; msgbox %CurrentTabURL% = %URL%
|
||
|
return
|
||
|
}
|
||
|
|
||
2 years ago
|
try, LastActiveTab := driver.URL
|
||
|
|
||
2 years ago
|
; / loop through tabs
|
||
|
}
|
||
|
Message = ActivateChromeTab function failed to activate tab for %urlBase% after looping through %NumberOfTabActivationLoops% tabs
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
return "Failed"
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
CheckForAlerts(){
|
||
|
Message = Checking for Any Obstructing Alerts in Chrome
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
|
||
|
|
||
|
status := 1
|
||
|
try driver.SwitchToAlert()
|
||
|
catch e {
|
||
|
status := 0
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
if(status){
|
||
|
; msgbox, alert found
|
||
|
; Message = Page Alert Found. Dismissing.
|
||
|
Message = Page Alert Found. Accepting.
|
||
|
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-------------------------------
|
||
|
|
||
1 year ago
|
|
||
|
; -------------------------------SaveDriverURL-------------------------------
|
||
2 years ago
|
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
|
||
|
}
|
||
|
|
||
|
|
||
|
; -------------------------------SChrome_Get-------------------------------
|
||
|
SChrome_Get(URL := "", Profile := "Profile 1", IP_Port := "127.0.0.1:9222"){
|
||
1 year ago
|
Message = Trying to Connect to Chrome
|
||
|
SaveOrPostProgress(Message:=Message,PostType:="Tooltip")
|
||
|
|
||
2 years ago
|
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.`nAutomatically 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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
if(ChromeFilepath = ""){
|
||
|
GetChromeFilepath()
|
||
|
}
|
||
2 years ago
|
|
||
1 year ago
|
if(!winExist("ahk_exe chrome.exe")){
|
||
|
run, %ChromeFilepath% --remote-debugging-port=%IP_Port_Nr% %URL%
|
||
|
}
|
||
2 years ago
|
|
||
1 year ago
|
Driver := ComObjCreate("Selenium.ChromeDriver")
|
||
|
Driver.SetCapability("debuggerAddress", IP_Port)
|
||
|
try Driver.Start()
|
||
|
catch e {
|
||
|
ShowSeleniumErrorMsgbox()
|
||
|
} ; end of catch
|
||
2 years ago
|
|
||
1 year ago
|
; Save current chrome version to ini file
|
||
|
return Driver
|
||
|
}
|
||
2 years ago
|
|
||
1 year ago
|
; -------------------------------/SChrome_Get-------------------------------
|
||
2 years ago
|
/*
|
||
|
*/
|
||
|
|
||
|
|
||
|
; 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
|
||
|
}
|
||
|
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
|
||
|
}
|
||
|
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
|
||
|
}
|
||
|
Return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
Return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
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
|
||
|
}
|
||
|
|
||
|
|
||
2 years ago
|
;---\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
|
||
|
}
|
||
|
}
|
||
1 year ago
|
|
||
|
|