Jump to content
Omnitica

Problems with _IEFormElementSetValue and javascript

Recommended Posts

Omnitica

Hello!

I'm new to the forums! Couldn't find any threads covering this issue. Sorry if I'm wrong.


I'm having problems changing the text/value of a input box. The particular box doesn't have a name or id, just: class="jq_hotkeys_enabled form-control".
This class appears multiple times throughout the source, so I don't think It's right to reference that object. 
See image 1.

However, I've managed to find a object with the name "ctl00$content$ctrl$ucServiceTime$rpServicetimes$ctl00$edtHiddenResourceHelper" which contains the value of the input box, and the id for the person in the dropdown/combobox left of it. (with "dummy-1" selected).
See image 2.

As you can see the value is 3982,4545     ( name , time )

But when I try to change the value of it by doing:

#include <IE.au3>

Local $oIE = _IEAttach(WinGetHandle("[CLASS:IEFrame]"),"embedded")
Local $oForm = _IEFormGetObjByName($oIE, "aspnetForm")
Local $oInput = _IEFormElementGetObjByName($oForm, "ctl00$content$ctrl$ucServiceTime$rpServicetimes$ctl00$edtHiddenResourceHelper")
_IEFormElementSetValue($oInput, "3982,1337")

The box doesn't get its new value. I  can see in the source that it's changed though.
See image 3.

 

However, if I do: 

#include <IE.au3>

Local $oIE = _IEAttach(WinGetHandle("[CLASS:IEFrame]"),"embedded")
Local $oForm = _IEFormGetObjByName($oIE, "aspnetForm")
Local $oInput = _IEFormElementGetObjByName($oForm, "ctl00$content$ctrl$ucServiceTime$rpServicetimes$ctl00$edtHiddenResourceHelper")
_IEFormElementSetValue($oInput, "3982,1337")

$oIE.document.parentwindow.execScript("theForm.submit()") ; But it needs to reload the page

The box gets its new value after the page reloads by the theForm.submit() function. 
This is a bit unfortunate as I want the changes to happen instantly like when I type in the box with the keyboard... or select in the combobox.

Maybe someone knows how I can do this? Looking forward to an answer! 
Thanks in advance.

 

Note: Unfortunately I can't provide with the real URL as it is constricted to employees in the company I work for, but let me know If I should provide anything else.

 

1.PNG

2.PNG

3.PNG

Share this post


Link to post
Share on other sites
Danp2

The site is likely using a Javscript framework, such as Node.js or React. These will obscure the code, use hidden objects, etc like you are seeing. This is probably going to be difficult for us to assist you with since the site is private.

First step would be to identify the framework being used.

  • Like 1

Share this post


Link to post
Share on other sites
Omnitica
18 minutes ago, Danp2 said:

The site is likely using a Javscript framework, such as Node.js or React. These will obscure the code, use hidden objects, etc like you are seeing. This is probably going to be difficult for us to assist you with since the site is private.

First step would be to identify the framework being used.

Thanks for the quick reply! 

Sent you a P.M

Share this post


Link to post
Share on other sites
Omnitica
21 minutes ago, Danp2 said:

There are jQuery events attached to the element. You will need to trigger the Change event via jQuery. I'll get back to you if I come up with a workable solution.

Thank you Danp2. I appreciate the effort.

Share this post


Link to post
Share on other sites
Danp2

This (possible) solution came from this prior discussion. I'm unable to get this page to render properly in IE. Therefore, I'm unable to test the following code and you will likely need to adjust or adapt it to fit your actual environment --

#include <ie.au3>

Local $oIE = _IEAttach(WinGetHandle("[CLASS:IEFrame]"),"embedded")
Local $oDocument = _IEDocGetObj($oIE)
Local $oWindow = $oDocument.ParentWindow

$oWindow.setTimeout("document.body.JSeval = eval; ", 0) ; a reference to the eval method

Sleep(2000) ; give a little time to the browser to create above variables within the javascript environment.

$JSeval = $oIE.Document.body.JSeval ; a reference to the eval javascript method from within AutoIt

$JSeval('$(".input-group .form-control:not(:first-child):not(:last-child)")[0] = 10')
$JSeval('$(".input-group .form-control:not(:first-child):not(:last-child)").change()')

 

Share this post


Link to post
Share on other sites
Omnitica
13 hours ago, Danp2 said:

This (possible) solution came from this prior discussion. I'm unable to get this page to render properly in IE. Therefore, I'm unable to test the following code and you will likely need to adjust or adapt it to fit your actual environment --

#include <ie.au3>

Local $oIE = _IEAttach(WinGetHandle("[CLASS:IEFrame]"),"embedded")
Local $oDocument = _IEDocGetObj($oIE)
Local $oWindow = $oDocument.ParentWindow

$oWindow.setTimeout("document.body.JSeval = eval; ", 0) ; a reference to the eval method

Sleep(2000) ; give a little time to the browser to create above variables within the javascript environment.

$JSeval = $oIE.Document.body.JSeval ; a reference to the eval javascript method from within AutoIt

$JSeval('$(".input-group .form-control:not(:first-child):not(:last-child)")[0] = 10')
$JSeval('$(".input-group .form-control:not(:first-child):not(:last-child)").change()')

 

Hey, thank you for your help. Again, much appreciated.

I'm guessing I need to add Local $oDocument = _IEDocGetObj($oIE)  to get a reference.

I'm having trouble understanding the java functions: 

$JSeval('$(".input-group .form-control:not(:first-child):not(:last-child)")[0] = 10')
is this the function that sets the value of the box?

 

$JSeval('$(".input-group .form-control:not(:first-child):not(:last-child)").change()')
And this is the function that trigger the change?

Share this post


Link to post
Share on other sites
Danp2

Yes, the first call sets the value of the input element. This could also be done with _IEFormElementSetValue if you can get a reference to the element. The second call triggers the change event for all matching elements.

Did you try running the code as posted? If so, what was the outcome?

Share this post


Link to post
Share on other sites
Omnitica
2 minutes ago, Danp2 said:

Yes, the first call sets the value of the input element. This could also be done with _IEFormElementSetValue if you can get a reference to the element. The second call triggers the change event for all matching elements.

Did you try running the code as posted? If so, what was the outcome?

I tried running the script, but no success . The value doesn't change. However I'm curious if the 
$JSeval('$(".input-group .form-control:not(:first-child):not(:last-child)")[0] = 10')
refers to the actual input box. As there are many boxes with the same class. Maybe I'm wrong.

Share this post


Link to post
Share on other sites
Danp2

You could try this yourself if you can open the page in a non-embedded IE. Hit F12 to open the developers tools. Then try entering the command

$(".input-group .form-control:not(:first-child):not(:last-child)")[0] = 10

from the console. If that doesn't work, then perhaps the page is rendering differently under IE (I used FF to come up with the CCS selector).

Share this post


Link to post
Share on other sites
Omnitica

$(".input-group .form-control:not(:first-child):not(:last-child)").change()

Strangely enough this sets the value of the

<input type="hidden" name="ctl00$content$ctrl$ucServiceTime$rpServicetimes$ctl00$edtHiddenResourceHelper" id="content_ctrl_ucServiceTime_rpServicetimes_edtHiddenResourceHelper_0" value="4448,6767">

to the value that is in the input box, which is 6767. Kind of opposite of what we're trying to do.

 

Also,

$(".input-group .form-control:not(:first-child):not(:last-child)")[0] = 10

Doesn't change the value for me, somehow. But I can still use  _IEFormElementSetValue which I know works.

Edited by Omnitica

Share this post


Link to post
Share on other sites
Danp2

Which element are you updating with IEFormElementSetValue? If you mean,

ctl00$content$ctrl$ucServiceTime$rpServicetimes$ctl00$edtHiddenResourceHelper

then that probably isn't the optimal solution and may not work as expected.

Try this one from the developer console --

$('.input-group-addon:not(:first-child):not(:last-child), .input-group-btn:not(:first-child):not(:last-child), .input-group .form-control:not(:first-child):not(:last-child)')[0].value=10

 

Share this post


Link to post
Share on other sites
Omnitica
7 minutes ago, Danp2 said:

Which element are you updating with IEFormElementSetValue? If you mean,

ctl00$content$ctrl$ucServiceTime$rpServicetimes$ctl00$edtHiddenResourceHelper

then that probably isn't the optimal solution and may not work as expected.

Try this one from the developer console --

$('.input-group-addon:not(:first-child):not(:last-child), .input-group-btn:not(:first-child):not(:last-child), .input-group .form-control:not(:first-child):not(:last-child)')[0].value=10

 

Yes! Now we're getting somewhere. The value changes. But now, how to change the first part of the value,  value="4448,6767">. Or rather the combobox that is before the inputbox.

Note: I was previously using the 

ctl00$content$ctrl$ucServiceTime$rpServicetimes$ctl00$edtHiddenResourceHelper

for  _IEFormElementSetValue 

Edited by Omnitica

Share this post


Link to post
Share on other sites
Danp2

Now try entering

$('.input-group-addon:not(:first-child):not(:last-child), .input-group-btn:not(:first-child):not(:last-child), .input-group .form-control:not(:first-child):not(:last-child)').change()

This should cause the change event to trigger, force the value to reflect properly as if you had typed it in manually.

What have you tried with the combobox?

Share this post


Link to post
Share on other sites
Omnitica
27 minutes ago, Danp2 said:

Now try entering

$('.input-group-addon:not(:first-child):not(:last-child), .input-group-btn:not(:first-child):not(:last-child), .input-group .form-control:not(:first-child):not(:last-child)').change()

This should cause the change event to trigger, force the value to reflect properly as if you had typed it in manually.

What have you tried with the combobox?

With this the:


<input type="hidden" name="ctl00$content$ctrl$ucServiceTime$rpServicetimes$ctl00$edtHiddenResourceHelper" id="content_ctrl_ucServiceTime_rpServicetimes_edtHiddenResourceHelper_0" value="4448,6767">

Changes accordingly whats was changed into the box. Great!

 

But now I'm looking for a way to change the option in the combobox, which is defined by  value="4448,6767">   (4448)
4448 is the id of a user.

I just used  _IEFormElementSetValue  on the  ctl00$content$ctrl$ucServiceTime$rpServicetimes$ctl00$edtHiddenResourceHelper.
And set the value to "4448,6767" But then I need to reload the page for the changes to happen.

A similar approach like the 

$('.input-group-addon:not(:first-child):not(:last-child), .input-group-btn:not(:first-child):not(:last-child), .input-group .form-control:not(:first-child):not(:last-child)')[0].value=10

perhaps.

Edited by Omnitica

Share this post


Link to post
Share on other sites
Danp2

Try the following and let me know if it works --

#include <ie.au3>

Local $oIE = _IEAttach("UR-000000")
Local $oDocument = _IEDocGetObj($oIE)
Local $oWindow = $oDocument.ParentWindow

$oWindow.setTimeout("document.body.JSeval = eval; ", 0) ; a reference to the eval method

Sleep(2000) ; give a little time to the browser to create above variables within the javascript environment.

$JSeval = $oIE.Document.body.JSeval ; a reference to the eval javascript method from within AutoIt

$oSelect = $JSeval('$(".bootstrap-select > select")[0]')
_IEFormElementOptionSelect($oSelect, "4673")

 

Share this post


Link to post
Share on other sites
Omnitica

Hmm, did not work to change in the actual combobox, but the value changes in

<input name="ctl00$content$ctrl$ucServiceTime$rpServicetimes$ctl00$edtHiddenResourceHelper" id="content_ctrl_ucServiceTime_rpServicetimes_edtHiddenResourceHelper_0" type="hidden" value="4673,5656">

 

Edited by Omnitica

Share this post


Link to post
Share on other sites
Omnitica

However, doing a

#include <ie.au3>

Local $oIE = _IEAttach("UR-000000")
Local $oDocument = _IEDocGetObj($oIE)
Local $oWindow = $oDocument.ParentWindow

$oWindow.setTimeout("document.body.JSeval = eval; ", 0) ; a reference to the eval method

Sleep(2000) ; give a little time to the browser to create above variables within the javascript environment.

$JSeval = $oIE.Document.body.JSeval ; a reference to the eval javascript method from within AutoIt

$oSelect = $JSeval('$(".bootstrap-select > select")[0]')
_IEFormElementOptionSelect($oSelect, "4673")
$JSeval('$(".bootstrap-select > select").change()')

solved it!

Share this post


Link to post
Share on other sites
Danp2

Cool... glad you were able to figure it out. That likely would have been my next suggestion. ;-)

The issue is that the display value is being shown in a Span element, not in the actual Select element.

Share this post


Link to post
Share on other sites
Omnitica

Yes indeed.

 

Thank you so much for your effort Danp2, you really helped me out here :)

Now I can get back to finishing my script!

Have a good day / evening.

 

Cheers!

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • Seminko
      By Seminko
      Wrote a script that grabs all of the IP addresses from Netflix's IP log, checks the IPs and returns suspicious activity.
      Everything works as it should but only when _IECreate is set to visible. When visible is set to false, it fails to login for some reason.
      Any ideas what might cause it and/or how to circumvent that?
    • nooneclose
      By nooneclose
      I need to send a string of text to this popup and click on the ok button to save it.
      Here is the code I have so far:
      ;Start IE Sleep(7000) $oIE = _IECreate("http://www.google.com") Sleep(500) _IELoadWait($oIE) $hIE = _IEPropertyGet($oIE, "hwnd") ; Get Handle of the IE window Sleep(500) WinSetState($hIE, "", @SW_MAXIMIZE) ;Wait for a browser page load to complete Sleep(3000) _IENavigate($oIE, "https://properURL.com") Sleep(8000) _IELoadWait($oIE) ;Attach to a browser control embedded in another window $oIE = _IEAttach("https://"properURL.com", url") ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oIE = ' & $oIE & @CRLF & '>Error code: ' & @error & ' Extended code: 0x' & Hex(@extended) & @CRLF) ;### Debug Console Sleep(2000) ;Get the title of the webpage ;Local $wTitle = _IEPropertyGet($oIE, "title") ;MsgBox($MB_SYSTEMMODAL, "Webpage title:", $wTitle) ;Clicks the new button Sleep(3000) _IEAction($nWorkOrderB, "focus") _IEAction($nWorkOrderB, "click") Sleep(5000) ;Store the Element names where the important data will be sent ;Store the long description button Local $wLongDButton = _IEGetObjById($oIE, "m65d795a4-img") ;Store the long Description field id Local $wComments = _IEGetObjById($oIE, "ma6499a9c-rte_iframe") ;Store the ok button id that is in the long description Local $wCommOk = _IEGetObjById($oIE, "m74031266-pb") ;******************************************************************************* ; Send the stored data to the proper field ;******************************************************************************* ;Click the long description button Sleep(300) _IEAction($wLongDButton, "focus") _IEAction($wLongDButton, "click") Sleep(300) ;Sends the Comments Sleep(500) _IEAction($wComments, "focus") _IEAction($wComments, "click") Sleep(500) _IEFormElementSetValue($wComments, "hello darkness my old friend") ;Click the ok button Sleep(500) _IEAction($wCommOk, "focus") _IEAction($wCommOk, "click") Sleep(500)  
      Here is the popup:

    • hemichallenger
      By hemichallenger
      Hello,
      If anyone can help, it would be greatly appreciated. The code is just an example and similar to the issue with an internal webpage. I'm trying to autofill than click the submit button. I get the same error running the script.
      _IEFormElementSetValue, $_IESTATUS_InvalidObjectType
      IEGetObjById, $_IESTATUS_NoMatch
      Is it possible to edit the <textarea></textarea>  field with AutoIt?  If anyone could assist me on how. Than I could have a better understanding and chance in figuring it out on my main script. Thank you
      #include <IE.au3> Local $oIE1 = _IECreate ("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_textarea") _IELoadWait($oIE1) local $oFormCollection = _IEFormGetCollection($oIE1,0) local $oid = _IEGetObjByid($oFormCollection, "iframeResult") _IEFormElementSetValue($oid, "test") sleep (2000) Local $oIE2 = _IECreate ("https://www.w3schools.com/html/tryit.asp?filename=tryhtml_scripts_intro") _IELoadWait($oIE2) $oForm2 = _IEFormGetCollection($oIE2,0) $oClickMe = _IEGetObjById($oForm2, "demo") _IEAction($oClickMe, "focus") _IEAction($oClickMe, "click")
    • Blueman
      By Blueman
      Hey Guys,
      Hope that you can help me with something, maybe this is a bug in the new version of AUTOIT but first i will check it with you to know for sure.
      I have made a simple GUI with a Embedded IE Object, then i would like to read the HTML with _IEBodyReadHTML(), easy right?
      When i use the old IE.au3 include from a year back or so, it is working fine!
      When i use the new IE.au3 include came with the new installation that is currently available on autoitscript.com it isnt working (i get a result that says; 0).
      Let me show you.
       
      Working Example
      #include <GUIConstantsEx.au3> #include <IE_EmbeddedVersioning.au3> #include <IE_PreVersion.au3> ;Older Version Example() Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example", 1000, 1000) Local $idOK = GUICtrlCreateButton("OK", 310, 370, 85, 25) Global $oIE_1 = _IECreateEmbedded() ; CREATE IE OBJECT(S) GUICtrlCreateObj($oIE_1, 355, 5, 600, 360) _IENavigate($oIE_1, "https://www.google.nl", 1) Local $CheckHTML_T = _IEBodyReadHTML($oIE_1) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) MsgBox(48,"",$CheckHTML_T) ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idOK ExitLoop EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>Example  
      Failing Example
      #include <GUIConstantsEx.au3> #include <IE_EmbeddedVersioning.au3> #include <IE.au3> ;New Version Example() Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example", 1000, 1000) Local $idOK = GUICtrlCreateButton("OK", 310, 370, 85, 25) Global $oIE_1 = _IECreateEmbedded() ; CREATE IE OBJECT(S) GUICtrlCreateObj($oIE_1, 355, 5, 600, 360) _IENavigate($oIE_1, "https://www.google.nl", 1) Local $CheckHTML_T = _IEBodyReadHTML($oIE_1) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) MsgBox(48,"",$CheckHTML_T) ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idOK ExitLoop EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>Example  
      I have attachted all files and i am testing on Windows 10 with the latest SciTe Program (Not compiled). 
      When i compile the script it is showing the same result.
      Thanks guys!
      IE_PreVersion.au3
      IE.au3
      IE_EmbeddedVersioning.au3
    • Chimp
      By Chimp
      The DOM allows to do anything with elements and their contents, but first we need to reach the corresponding DOM object, get it into a variable, and then we are able to modify it. *
      Well, this little tool (although it is not very nice aesthetically) allows you to get visually a "selector" usable to reference DOM objects.
      Once you have the "selector" of an element you can pass it to the javascript querySelector() function that will return a reference to that element.
      To use this tool you have to:
      1) open the web page you want to inspect into IE browser
      2) run this script (if it find more instances of IE running, it allows you to chose one)
      3) move the mouse over the browser. The "selector" of the element below the pointer is catched automatically while hovering. To copy the selector in the clipboard just right click on the element.
      As you can see, while hovering, the element pointed by the mouse is highlighted with a thin red dotted frame to allow you to better "take aim"
      when the selector is copied to the clipboard a little acoustic signal is emitted as a confirm, then you can paste it in your listing where you need it.
      I hope it can come in handy and save you time when you need to automate a site .... have fun (debugged on Sept. 30 2018)
      #include <IE.au3> #include <GUIConstantsEx.au3> #include <GuiListBox.au3> #include <WindowsConstants.au3> #include <Misc.au3> ; for _IsPressed (23 END key) Global $hDLL = DllOpen("user32.dll") ; following global variables are automatically updated by events from the browser ; ------------------------------------------------------------------------------------- Global $g_iMouseX, $g_iMouseY ; coordinates of the mouse while mooving over the browser Global $bCopySelector = False ; becomes True when you right click on wanted element ; ------------------------------------------------------------------------------------- Global $oIE = _Get_IE() ; get IE instance to inspect If IsObj($oIE) Then $hIE = _IEPropertyGet($oIE, "hwnd") WinActivate($hIE) _InspectElements() EndIf DllClose($hDLL) Exit Func _InspectElements() ; it uses the global variable $oIE as source ; --- set IE to interact with AutoIt --- Local $oDocument Do ; wait for document Sleep(250) $oDocument = $oIE.document Until IsObj($oDocument) Local $oWindow = $oDocument.ParentWindow ; create a reference to the javascript eval method ; in the body section of the dovument $oWindow.setTimeout("document.body.JSeval = eval; ", 0) ; attach the $JSeval variable to the javascript eval method Local $JSeval Do $JSeval = Execute('$oIE.Document.body.JSeval') Until IsObj($JSeval) ; --------------------------------------------- ; Inject Javascript functions/elements to $oIE ; --------------------------------------------- ; Get the DOM path of an element (a CSS selector) ; ----------------------------------------------- ; This javascript function returns the CSS selector of the passed element. ; You can then use the returned path to get a reference to the pointed ; element by the QuerySelector() javascript function ; function copied from the following link: ; https://stackoverflow.com/questions/5728558/get-the-dom-path-of-the-clicked-a ; see answer by "Aleksandar Totic" (thanks to him) Local $sJScript = "" & _ " function getDomPath(el) {" & _ " if (!el) {" & _ " return;" & _ " }" & _ " var stack = [];" & _ " var isShadow = false;" & _ " while (el.parentNode != null) {" & _ " var sibCount = 0;" & _ " var sibIndex = 0;" & _ " for ( var i = 0; i < el.parentNode.childNodes.length; i++ ) {" & _ " var sib = el.parentNode.childNodes[i];" & _ " if ( sib.nodeName == el.nodeName ) {" & _ " if ( sib === el ) {" & _ " sibIndex = sibCount;" & _ " }" & _ " sibCount++;" & _ " }" & _ " }" & _ " var nodeName = el.nodeName.toLowerCase();" & _ " if (isShadow) {" & _ " nodeName += ""::shadow"";" & _ " isShadow = false;" & _ " }" & _ " if ( sibCount > 1 ) {" & _ " stack.unshift(nodeName + ':nth-of-type(' + (sibIndex + 1) + ')');" & _ " } else {" & _ " stack.unshift(nodeName);" & _ " }" & _ " el = el.parentNode;" & _ " if (el.nodeType === 11) {" & _ " isShadow = true;" & _ " el = el.host;" & _ " }" & _ " }" & _ " stack.splice(0,1);" & _ " return stack.join(' > ');" & _ " }" ; more infos here: https://www.kirupa.com/html5/finding_elements_dom_using_querySelector.htm ; Inject the above javascript function contained in the $sJScript variable into the document _JS_Inject($oIE, $sJScript) Local $_getDomPath ; a reference to call above function from AutoIt Do Sleep(250) $_getDomPath = $jsEval("getDomPath") Until IsObj($_getDomPath) ; ; ------------------- ; hook some IE events ; ------------------- Local $oEventObjects[2], $oEventsSource $oEventsSource = $oIE.document.documentElement ; element we want catch events from ; https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa769636(v=vs.85) $oEventObjects[0] = ObjEvent($oEventsSource, "_HTMLElementEvents2_", "HTMLElementEvents2") ; https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa768283(v%3dvs.85) $oEventObjects[1] = ObjEvent($oIE, "_IEEvent_", "DWebBrowserEvents2") ; open a GUI where to show some element's properties ; -------------------------------------------------- Local $hGUIMain = GUICreate("Info", 500, 140, -1, -1, -1, $WS_EX_TOPMOST) Local $hProperties = GUICtrlCreateEdit("", 0, 0, 500, 140) GUICtrlSetFont(-1, 9, -1, -1, "Courier New") GUISetState() ;Show GUI ; -------------------------------------------------- ; --------- ; Main loop ; --------- Local $iMouseX, $iMouseY, $oElement, $oNewElement, $sSelector Local $oGotElement, $sElementInfos Local $sSaved_StyleOutline, $sSaved_StyleOutline2 ; Loop until the user exits. While IsObj($oIE) Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop ; ---> end EndSwitch If ($g_iMouseX <> $iMouseX) Or ($g_iMouseY <> $iMouseY) Then $iMouseX = $g_iMouseX $iMouseY = $g_iMouseY ; $oElement = $oIE.document.elementFromPoint($iMouseX, $iMouseY) ; <-- this way is slower $oNewElement = $JSeval('document.elementFromPoint(' & $iMouseX & ',' & $iMouseY & ');') If $oNewElement <> $oElement Then If IsObj($oElement) Then $oElement.style.outline = $sSaved_StyleOutline $oElement = $oNewElement ; $bSelfie = False ; $iSelf_Timer = TimerInit() $sSaved_StyleOutline = $oElement.style.outline ; save new element's original outline style $sSelector = $_getDomPath($oElement) ; get CSS path If $sSelector <> "" Then ; We could use the $oNewElement, but just to proof that $sSelector is OK ; we get again a reference to the new pointed element using it's $sSelector $oGotElement = $JSeval('document.querySelector("' & $sSelector & '");') ; <-- how to use a selector $oGotElement.style.outline = "1px dashed red" ; mark new pointed element ; https://css-tricks.com/ $sElementInfos = "" & _ "nodeName: " & $oGotElement.nodeName & @CRLF & _ "id: " & $oGotElement.getAttribute('id') & @CRLF & _ "class: " & $oGotElement.getAttribute('class') & @CRLF & _ "type: " & $oGotElement.getAttribute('type') & @CRLF & _ "---------" & @CRLF & _ $sSelector ControlSetText($hGUIMain, "", $hProperties, $sElementInfos) EndIf EndIf EndIf ; $bCopySelector is setted to True by the right-click event on an element, ; see Volatile Func _HTMLElementEvents2_onContextmenu($oEvent) near script bottom If $bCopySelector And ($sSelector <> "") Then ; And (TimerDiff($iSelf_Timer) > $bSelfie_Delay) Then ; $sSaved_StyleOutline2 = $oGotElement.style.outline $oGotElement.style.outline = "5px dotted #ff0066" ; mark copied element ClipPut($sSelector) $sElementInfos &= @CRLF & "selector copied to ClipBoard" ControlSetText($hGUIMain, "", $hProperties, $sElementInfos) Beep(2000, 50) $bCopySelector = False Sleep(250) $oGotElement.style.outline = $sSaved_StyleOutline2 ; ToolTip('') EndIf If _IsPressed("23", $hDLL) Then ; END key pressed If IsObj($oElement) Then $oElement.style.outline = $sSaved_StyleOutline WinActivate($hGUIMain) ; WinSetState($hGUIMain, "", @SW_SHOW) $aWin = WinGetPos($hGUIMain) MouseMove($aWin[0] + $aWin[2] / 2, $aWin[1] + $aWin[3] / 2, 0) EndIf WEnd ; the end ; ------------------------------------------ For $i = 0 To UBound($oEventObjects) - 1 ; Tell IE we don't want to receive events. $oEventObjects[$i] .Stop $oEventObjects[$i] = 0 Next $oIE = 0 ; Remove IE from memory GUIDelete($hGUIMain) ; Remove GUI ; ------------------------------------------ EndFunc ;==>_InspectElements Func _Get_IE() ; Example 5 from the _IEAttach help ; Create an array of object references to all current browser instances ; The first array element will contain the number of instances found Local $aIE[1] $aIE[0] = 0 Local $i = 1, $oIEx While 1 $oIEx = _IEAttach("", "instance", $i) If @error = $_IEStatus_NoMatch Then ExitLoop ReDim $aIE[$i + 1] $aIE[$i] = $oIEx $aIE[0] = $i $i += 1 WEnd If $aIE[0] > 0 Then If $aIE[0] = 1 Then Return $aIE[1] ; only one IE is running, return this then ; ; Create a little list box to choose the IE instance from Local $hChoose_IE = GUICreate("IE Instances", 600, 350) Local $Label1 = GUICtrlCreateLabel($aIE[0] & " running Instances of IE browser found, click the one you want to attach to then click on 'ok'", 5, 5, 590, 20) Local $List1 = GUICtrlCreateList("", 5, 30, 590, 300, BitOR($LBS_STANDARD, $LBS_EXTENDEDSEL)) Local $hButton_choosed = GUICtrlCreateButton("OK", 5, 325, 590, 20) For $i = 1 To $aIE[0] GUICtrlSetData($List1, $i & ") " & _IEPropertyGet($aIE[$i], "locationurl")) Next GUISetState(@SW_SHOW) While 1 ; wait for a selection Switch GUIGetMsg() Case $GUI_EVENT_CLOSE GUIDelete($hChoose_IE) Return False Case $hButton_choosed $aSelected = _GUICtrlListBox_GetSelItems($List1) If $aSelected[0] Then GUIDelete($hChoose_IE) Return $aIE[$aSelected[1] + 1] Else MsgBox(0, "Info", "Please select an item") EndIf EndSwitch WEnd Else MsgBox(0, 'error', "Sorry" & @CRLF & @CRLF & "no running IE instances found") EndIf EndFunc ;==>_Get_IE ; this function creates a javascript script into the html document ; of the passed $oIE object using the createElement method. Func _JS_Inject($oIE, $sJScript, $bIsUrl = False) ; ; get a reference to the document object Local $objDocument = $oIE.document ; Local $oScript = $objDocument.createElement('script') ; $oScript.type = 'text/javascript' If $bIsUrl Then $oScript.src = $sJScript ; works if $sJScript is a link to a js listing (url) Else ; (https://stackoverflow.com/questions/35213147/difference-between-text-content-vs-inner-text) ; $oScript.innerText = $sJScript $oScript.TextContent = $sJScript ; works if $sJScript contains the listing itself EndIf ; $objDocument.getElementsByTagName('head').item(0).appendChild($oScript) ; $objDocument.getElementsByTagName('head').item(0).removeChild($oScript); ; EndFunc ;==>_JS_Inject ; ------------------------------------------------------------------- ; following function(s) are called by registered $oIE elements events ; ------------------------------------------------------------------- ; ; The function automatically fired by an event ; will receive as parameter an Event Obj. ; This obj has properties related to ; the object that fired the event. ; See following link: ; https://msdn.microsoft.com/en-us/library/aa703876(v=vs.85).aspx ; function called by the mousemove event ; we use this to update 2 global variables: Volatile Func _HTMLElementEvents2_onMousemove($oEvent) $g_iMouseX = $oEvent.clientX $g_iMouseY = $oEvent.clientY EndFunc ;==>_HTMLElementEvents2_onMousemove ; function called by the contextmenu event ; we use this to update 1 global variable ; and we also neutralize this event: Volatile Func _HTMLElementEvents2_onContextmenu($oEvent) $oEvent.cancelBubble = True ; event propagation cancelled $oEvent.returnValue = False ; prevent default behaviour $bCopySelector = True ; when True, selector will be copied to clipboard in main loop EndFunc ;==>_HTMLElementEvents2_onContextmenu ; https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa768280%28v%3dvs.85%29 Func _IEEvent_BeforeNavigate2($oIEpDisp, $sIEURL, $iIEFlags, $sIETargetFrameName, $sIEPostData, $iIEHeaders, $bIECancel) ;ConsoleWrite("Debug: navigate away cancelled." & @CRLF) ; https://stackoverflow.com/questions/6526876/how-to-cancel-or-dispose-current-navigation-at-webbrowser-element $oIE.stop EndFunc ;==>_IEEvent_BeforeNavigate2 Here is a simple example on how a "selector" can be used in AutoIt.
      suppose we want automate the login to the AutoIt site with our username and password.
      I've already prepared a very simple "template" where are missing some important parts without which the script can't work. Missing parts are the references to the elements of the AutoIt web page that we have to manage by our script.
      well, here is where the tool I have just posted here above comes to our help.
      follow this steps:
      1) in IE open the AutoIt site at the forum page (https://www.autoitscript.com/forum/)
      2) run the above tool (select the IE instance and/or bring it to front if needed)
      3) when the script is "ready", move the mouse over the "Existing user? Sign In" string and right click the mouse button. Doing so the "selector" of that element is copied to the clipboard. Now we can paste it in our AutoLogIt.au3 script as value of the $sSignIn variable.
      4) now click on the "Existing user? Sign In"  to open the "Sig In" session from where we will copy selectors of each of the 2 input box Username and Password, in the same way as we have already done in step 3, and paste those selectors to the $sInputUserId and $sInputPasswd variables respectively.
      5) do the same for the "Sign In" Button and paste it's selector to the $sSignInButn variable
      6) of course also fill the $sMyUserId and $sMyPasswd variables with your data.
      That's It. Run the AutoLogIt script and it should Log you on automatically to the forum.
      AutoLogIt.au3
      #include <ie.au3> $sMyUserId = "" ; <-- your userid here $sMyPasswd = "" ; <-- your password here ; set selectors here $sSignIn = "" ; <-- SigIn element selector here $sInputUserId = "" ; <-- UserId input selector here $sInputPasswd = "" ; <-- Password input selector here $sSignInButn = "" ; <-- Sig In button selector here $oIE = _IECreate("https://www.autoitscript.com/forum/") ; here is how to use the QuerySelector javascript function $hDOM_Element = $oIE.document.QuerySelector($sSignIn) ; get the "sign in" link element ; perform a click action on the above element $hDOM_Element.click() ; or _IEAction($hDOM_Element, "click") as well ; fill the username input $hDOM_Element = $oIE.document.QuerySelector($sInputUserId) $hDOM_Element.value = $sMyUserId ; fill the password input $hDOM_Element = $oIE.document.QuerySelector($sInputPasswd) $hDOM_Element.value = $sMyPasswd ; .... or also using the dot notation directly .... $oIE.document.QuerySelector($sSignInButn).click() Sleep(5000) ; this should logout $sMenu = "body > div:nth-of-type(2) > header > div > ul > li:nth-of-type(6) > a:nth-of-type(2)" $oIE.document.QuerySelector($sMenu).click() $sLogOut = "body > ul > li:nth-of-type(9) > a" $oIE.document.QuerySelector($sLogOut).click()  
×