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.
video-uploader/Lib/ChromeAutomationFunctions.ahk

364 lines
12 KiB
Plaintext

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 ""
}
2 years ago
; 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 {
2 years ago
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
}
}
;---\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
}
} ; end of fuctions
OnMsgBoxJSReturnData() {
DetectHiddenWindows, On
Process, Exist
If (WinExist("ahk_class #32770 ahk_pid " . ErrorLevel)) {
ControlSetText Button1, OK
ControlSetText Button2, Copy
ControlSetText Button3, Return
}
}