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.

964 lines
27 KiB
Plaintext

; Notes/Extra Info/#Includes
;------------------------------------------------
; Selenium Related Functions
global MouseClicksSaved
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")
MsgBox 0x40043, Error, Failed to Connect to Chrome. `nPossible Causes for this:`n1) One of your tabs is stuck loading/refreshing. (Try closing all tabs and restarting) `n2) Chrome has a dialogue box open in one of your tabs. `n3) ChromeDriver needs to be updated.`n`nClick "Restart" to try the Upload 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% `nChrome Version of Last Successfull Upload: %PreviousWorkingChromeVersion%
OnMessage(0x44, "")
IfMsgBox Yes, {
; Open Instructions Gitea page
if(InStr(A_ScriptName, "Video"))
URL = https://freedomain.dev/yuriy/video-uploader#installation
else,
URL = https://freedomain.dev/yuriy/social-media-poster#installation
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
}
}
; -------------------------------Tab Navigation & Activation-------------------------------
CheckSeleniumDriver(){
try Driver := ConnectToChrome("", 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 := ConnectToChrome("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
StartTime := A_TickCount
; 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
TotalTabLoops := TotalTabsFound + 2
Message = Total Pre-Existing Tabs in Chrome: %TotalTabsFound%`nTotal Times looping through tabs to create Chrome Tabs Array: %TotalTabLoops%
SaveOrPostProgress(Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
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)
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
Loop % TotalTabLoops {
try Title := Driver.Title
try URL := Driver.URL
if(Title != "")
DriverTitleArray.Push(Title)
if(URL != "")
ChromeTabsURLArray.Push(URL)
; 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
}
TabEndTime := A_TickCount - TabFoundStartTime
TabEndTimeArray .= TabEndTime . ","
; Message = Time to switch to new tab: %TabEndTime%`nCurrent Loop: %A_index%/%TotalTabsFound%
; SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")
TabFoundStartTime := A_TickCount
; TimeToGrabCurrentTab := A_TickCount - StartTime
; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab
; Stop page refresh if it's happening
; try driver.executeScript("return window.stop")
; TimeToGrabCurrentTab := A_TickCount - StartTime
; Msgbox % "TimeToGrabCurrentTab: " TimeToGrabCurrentTab
; 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
}
*/
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
}
/*
*/
CheckCurrentTabForCurrentSite(){
try CurrentTabURL := GetCurrentTabURlBase()
CheckForAlerts()
if(!InStr(CurrentTabURL, CurrentSite)){
Message = Chromedriver failed to switch to %CurrentSite%. Current Tab URL: %CurrentTabURL%
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar")
return "Failed"
}
else, {
Message = CheckCurrentTabForCurrentSite() passed successfully
SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile")
}
}
/*
*/
GetCurrentTabURLBase(){
try, TabURL := driver.url
TabURL := ExtractBaseURL(TabURL)
return TabURL
}
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)
; Message = Checking for URLBase: %URLBase% within ChromeTabsURLArray
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
; msgbox % HasSubstringVal(ChromeTabsURLArray, URLBase) . "`n`n is the status of URLbase in ChromeTabsURLArray"
; if URLBase is NOT a substring within any of the values in the array
if(!HasSubstringVal(ChromeTabsURLArray, URLBase))
{
; DevModeMsgBox("Creating new tab")
Message = Chrome tab does not exist in ChromeTabsURLArray. Creating new tab ;. Creating new tab with URL: %URL%
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
try, run "%ChromeFilepath%" "%URL%"
catch e {
Message = Trying to Run Chrome with URL failed for some reason. Wrong Chrome filepath?
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
}
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"){
try currentURL := GetCurrentTabURLBase()
Message = Failed to switch active tab to %URLBase%, currently on different site.
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
CheckForAlerts()
; create a new tab and try to switch to it again
Message = Creating new Chrome tab with URL: %URL%
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
try, run "%ChromeFilepath%" "%URL%"
catch e {
Message = Trying to Run Chrome with URL failed for some reason. Wrong Chrome filepath?
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
}
ChromeTabsURLArray.Push(URL) ; Append the new url to the array
NewTabCreated := 1
sleep, 1000
status := ActivateChromeTab(URL)
if(Status = "Failed"){
Message = Failed to activate tab for %urlBase% after 2 seperate attempts. Chrome stuck on a dialog box of some sort?
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
}
; -------------------------------ActivateChromeTab-------------------------------
ActivateChromeTab(URL){
; Extract Base URL if necessary
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 :=
NumberOfTabActivationLoops := ChromeTabsURLArray.Length()
; Message = NumberOfTabActivationLoops has been increased by 3
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
Message = Looping through tabs -> %NumberOfTabActivationLoops% <- times to find tab with URL: %urlBase%
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
try, StartingTabURL := driver.url
; Message := "NumberOfTabActivationLoops: " NumberOfTabActivationLoops
; DevModeMsgBox(Message)
; NumberOfTabLoops := Driver.Windows.Count + 2
; Message = Looping through tabs to activate with: %URLBase%
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
; create empty variable for loop
LastActiveTab :=
loop % NumberOfTabActivationLoops { ; loop through tabs to find matching tab
UStartTime := A_TickCount ; start time
try driver.SwitchToNextWindow()
; 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
; sleep, 1000
; msgbox, looping through tabs
try CurrentTabTitle := driver.window.title
try CurrentTabURL := driver.Url
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")
CheckForAlerts()
}
else, {
Message = Current Tab URL is DIFFERENT from starting URL after %A_index% tab switches
SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile")
}
if(LastActiveTab = CurrentTabURL){
Message = Chrome is on same tab URL after calling SwitchToNextWindow() function
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
CheckForAlerts()
}
}
if(InStr(CurrentTabURL, URLBase)){
TabURL := GetCurrentTabURLBase()
Message = Successfully Found and Activated Tab. URLBase: %urlBase% and currentTabURL: %TabURL%
; Message = Found Tab:.`n%CurrentTabTitle% = %PageTitle%`n%CurrentTabURL% = %URLBase%
; DevModeMsgBox(message)
TabFoundSuccessfully := 1
; msgbox %CurrentTabTitle% = %PageTitle%
; msgbox %CurrentTabURL% = %URL%
return
}
try, LastActiveTab := driver.URL
; / loop through tabs
}
Message = ActivateChromeTab function failed to activate tab for %urlBase% after looping through %NumberOfTabActivationLoops% tabs
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
return "Failed"
}
CheckForAlerts(){
Message = Checking for Any Obstructing Alerts in Chrome
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
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 so page gets refreshed.
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-------------------------------
; -------------------------------SaveDriverURL-------------------------------
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
}
; -------------------------------ConnectToChrome-------------------------------
ConnectToChrome(URL := "", Profile := "Profile 1", IP_Port := "127.0.0.1:9222"){
Message = Trying to Connect to Chrome
SaveOrPostProgress(Message:=Message,PostType:="Tooltip")
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
}
}
}
if(ChromeFilepath = ""){
GetInstalledChromeFilepath()
}
if(!winExist("ahk_exe chrome.exe")){
run, %ChromeFilepath% --remote-debugging-port=%IP_Port_Nr% %URL%
}
Driver := ComObjCreate("Selenium.ChromeDriver")
Driver.SetCapability("debuggerAddress", IP_Port)
try Driver.Start()
catch e {
ShowSeleniumErrorMsgbox()
} ; end of catch
; Save current chrome version to ini file
return Driver
}
; -------------------------------/ConnectToChrome-------------------------------
/*
*/
; 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
}
MouseClicksSaved += 1
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
}
MouseClicksSaved += 1
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
}
MouseClicksSaved += 1
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
}
MouseClicksSaved += 1
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
}
}
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
}
}
}
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
}
;---\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
}
}