Possible for ChromeExecute to pass var back to MS?
Moderators: Dorian (MJT support), JRL
Possible for ChromeExecute to pass var back to MS?
Need to collect info using javascript, but cannot find a way to pass this info back to MS please?
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: Possible for ChromeExecute to pass var back to MS?
Put the data in a div or similar.
Re: Possible for ChromeExecute to pass var back to MS?
Yes I did something similar, you just have to first read in the custom created div afterwards, which is not ideal, but should work or maybe put it in the clipboard, but that also has it downsides.
For now that should work,
Thank you Grovkillen
For now that should work,
Thank you Grovkillen
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: Possible for ChromeExecute to pass var back to MS?
Why isn't it ideal? I do this and it's very straight forward.
Re: Possible for ChromeExecute to pass var back to MS?
I'm only a beginner in javascript, but have since using MS had to learn a lot how html & javascript works, trying to build a recorder that records web actions with MS so first thing I want to do is to get the element under mouse hover with this java code:
Which seems to work good, still need to figure out how to convert the element back to something usefull and "element_under_mouse.id" at least give an ID if one is there of course, tested this on a button element.
Next thing would be to capture the mouse clicks and when mouse down it will run the javascript above (it is far from finished):
Now back to your question, every time you click it has to run the javascript above as well as reading the value back from the div with something like this:
This creates a button at the bottom which needs to be read back into MS with ChromeFindelement, but it seems like a lot of looping which can make it very slow if not done properly.
Code: Select all
elm = document.querySelectorAll(":hover");
box = elm[elm.length-1].getBoundingClientRect();
x = box.x
y = box.y
element_under_mouse =document.elementFromPoint(x, y)
Next thing would be to capture the mouse clicks and when mouse down it will run the javascript above (it is far from finished):
Code: Select all
Dialog>Dialog1
object Dialog1: TForm
Left = 340
Top = 128
HelpContext = 5000
BorderIcons = [biSystemMenu]
BorderStyle = bsDialog
Caption = 'CustomDialog'
ClientHeight = 1
ClientWidth = 200
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -14
Font.Name = 'MS Sans Serif'
Font.Style = []
FormStyle = fsStayOnTop
OldCreateOrder = True
ShowHint = True
OnTaskBar = True
PixelsPerInch = 120
TextHeight = 16
end
EndDialog>Dialog1
Show>Dialog1
OnEvent>KEY_DOWN,VK27,0,Stop
OnEvent>KEY_DOWN,VK01,0,MouseDown
Let>Monitor=1
Let>LM=0
Let>Txt=Mouse Moving...
Let>Txt2=Left Mouse Down
Label>MousePosCheck
GetCursorPos>nXPos,nYPos,csType
Let>Coor=%nXPos%,%nYPos%
IF>{(%Coor_old%=%Coor%) AND (%LM%="0")}
SetDialogProperty>Dialog1,,Caption,Coor
ENDIF
IF>{(%Coor_old%<>%Coor%) AND (%LM%="0")}
SetDialogProperty>Dialog1,,Caption,Txt
ENDIF
Let>Coor_old=Coor
Wait>0.1
Goto>MousePosCheck
SRT>Stop
//Let>Monitor=0
Exit
END>Stop
SRT>MouseDown
Let>LM=1
//reset key down handler
OnEvent>KEY_DOWN,VK01,,
//set key up handler
IF>{(%Monitor%="1") AND (%LM%="1")}
SetDialogProperty>Dialog1,,Caption,Txt2
OnEvent>KEY_UP,VK01,,MouseUp
ENDIF
END>MouseDown
SRT>MouseUp
Let>LM=0
//space is up - reset up handler
OnEvent>KEY_UP,VK01,,
//set key down handler
OnEvent>KEY_DOWN,VK01,0,MouseDown
END>MouseUp
Code: Select all
const btn = document.createElement("button");
btn.innerHTML = "Hello Button";
document.body.appendChild(btn);
ChromeExecute>session_id,alert(document.querySelector("body > button").innerText);
ChromeExecute>session_id,document.querySelector("body > button").innerText="testing";
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: Possible for ChromeExecute to pass var back to MS?
Or you could have a separate event that looks for this div. You don't need to do that in the main thread.
Re: Possible for ChromeExecute to pass var back to MS?
So you mean running concurrent scripts like in this thread?
https://www.mjtnet.com/forum/viewtopic ... nt#p25384
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: Possible for ChromeExecute to pass var back to MS?
Custom event is running its own thread yes.
Re: Possible for ChromeExecute to pass var back to MS?
Thank you Grovkillen for the Onevent>CUSTOM hint, below I have a working example
It will navigate to www.google.com and then will create a button on the bottom, you may have to scroll down to see it, after that if you click inside where you would type in your google query it will update the button text to the html element, someone might find this quite usefull, although it is far from perfect.
It will navigate to www.google.com and then will create a button on the bottom, you may have to scroll down to see it, after that if you click inside where you would type in your google query it will update the button text to the html element, someone might find this quite usefull, although it is far from perfect.
Code: Select all
Dialog>Dialog1
object Dialog1: TForm
Left = 340
Top = 128
HelpContext = 5000
BorderIcons = [biSystemMenu]
BorderStyle = bsDialog
Caption = 'CustomDialog'
ClientHeight = 1
ClientWidth = 200
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -14
Font.Name = 'MS Sans Serif'
Font.Style = []
FormStyle = fsStayOnTop
OldCreateOrder = True
ShowHint = True
OnTaskBar = True
PixelsPerInch = 120
TextHeight = 16
end
EndDialog>Dialog1
Show>Dialog1
OnEvent>KEY_DOWN,VK27,0,Stop
OnEvent>KEY_DOWN,VK01,0,MouseDown
OnEvent>CUSTOM,TriggerSub,DoIT,DoSub
SetDialogProperty>Dialog1,,Caption,... LOADING ...
Let>CHROMEDRIVER_EXE=c:\chromedriver.exe
ChromeStart>session_id
ChromeNavigate>session_id,url,https://www.google.com
SetFocus>Google - Google Chrome
WaitWindowFocused>Google - Google Chrome
WindowAction>1,Google - Google Chrome
Let>Create_Box=1
Let>Monitor=1
Let>LM=0
LabelToVar>JS_Create_Box,js
ChromeExecute>session_id,js
Let>js=
Let>Txt=Mouse Moving...
Let>Txt2=Left Mouse Down
Label>MousePosCheck
GetCursorPos>nXPos,nYPos,csType
Let>Coor=%nXPos%,%nYPos%
IF>{(%Coor_old%=%Coor%) AND (%LM%="0")}
SetDialogProperty>Dialog1,,Caption,Coor
ENDIF
IF>{(%Coor_old%<>%Coor%) AND (%LM%="0")}
SetDialogProperty>Dialog1,,Caption,Txt
ENDIF
Let>Coor_old=Coor
Wait>0.1
Goto>MousePosCheck
SRT>Stop
//Let>Monitor=0
Exit
END>Stop
SRT>MouseDown
Let>LM=1
//reset key down handler
OnEvent>KEY_DOWN,VK01,,
//set key up handler
IF>{(%Monitor%="1") AND (%LM%="1")}
LabelToVar>JS_Update_Box,js
ChromeExecute>session_id,js
SetDialogProperty>Dialog1,,Caption,Txt2
OnEvent>KEY_UP,VK01,,MouseUp
ENDIF
END>MouseDown
SRT>MouseUp
Let>LM=0
//space is up - reset up handler
OnEvent>KEY_UP,VK01,,
//set key down handler
OnEvent>KEY_DOWN,VK01,0,MouseDown
END>MouseUp
Label>Start
wait>0.01
Goto>Start
SRT>TriggerSub
Let>DoIT=TRUE
END>TriggerSub
SRT>DoSub
//do something
Let>LM=2
SetDialogProperty>Dialog1,,Caption,strResult
Let>DoIT=FALSE
END>DoSub
/*
JS_Create_Box:
var btn = document.createElement("button");
var htm = "strResult";
btn.innerHTML = htm;
document.body.appendChild(btn);
*/
/*
JS_Update_Box:
elm = document.querySelectorAll(":hover");
box = elm[elm.length-1].getBoundingClientRect();
x = box.x
y = box.y
element_under_mouse=document.elementFromPoint(x, y)
document.querySelector("body > button").innerText = element_under_mouse.outerHTML;
*/
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: Possible for ChromeExecute to pass var back to MS?
Happy to have helped.