Jump to content

_IEAction focus action does not work does not always work


JohnVH
 Share

Recommended Posts

This should be a simple thing except I'm having trouble with the _IEAction($oElement, "focus") statement in the code below. I'm guessing maybe it's a bug caused by the complexity of this website.

Here is my code:

for $i = 1 to $grocData[0][0]
  _IELinkClickByIndex($oFrame, $grocData[$i][2])
  $oForm = _IEFormGetObjByName ($oFrame, "formSlip")
  $oElement = _IEFormElementGetCollection($oForm, $i*2-1)
  _IEAction($oElement, "focus")
  Send($grocData[$i][3])
;do some stuff
  _IEAction($oElement, "blur");debug line 1
  Send("{PAUSE}");debug line 2
next

If I run this code with the 2 lines commented as debug lines removed and with nothing in the "do some stuff" piece it works as I would expect. However if I adjust the code slightly so that the element I'm specifically giving focus to ends up losing it's focus then subsequent iterations of the loop do not work because the _IEAction($oElement, "focus") does nothing and then my send command seems to send the information into limbo.

Think of this as a grocery list. The left side of the screen has links to all the items like apples, oranges, cereal etc. By the time it gets to this part of the code my array has what I want to buy, the link number of the specific item (for example for oranges I click on the 5th link), and how much of the product I want like 5 oranges.

This site has multiple frames and seems to use a dummy frame as a control frame. This loop is in a single frame but the click on the link executes javascript in this control frame. What happens is initially the right side of the screen is blank. I then click on oranges and this executes javascript which creates the "formSlip" form along with 2 controls in it, one of which is the quantity. So I set focus to the quantity and send how much I want. I now see I want 5 oranges for example on the right side of the screen. Then it clicks on the next link which adds 2 more elements to the "formSlip" form and I send the quantity I want so now I might see 5 oranges and 10 apples on the right hand side of the screen. And so on. It works fine like that. The javascript that is executed with the _IELinkClickByIndex command always either adds two more elements to the formSlip form or it creates the formSlip form with 2 elements in it. The links are in another form that remains static. Both forms are in the same frame.

I run into problems if my "do some stuff" does something that removes the focus from the quantity element on the right hand side that I specifically set focus to with the _IEAction command. So to try and duplicate this problem as simply as possible I removed the "do some stuff" and added the 2 debug lines. The pause simply executes a sleep to pause the script.

With the 2 debug lines in, the first time I execute a link the _IEAction works and the quantity is sent to that element and the script pauses. If I now unpause it without doing anything, then clicking on the second link correctly adds two more elements but the _IEAction does NOT work and therefore my send also does nothing so I see an empty quantity. And this will keep repeating if I simply keep unpausing the script. But the interesting thing is if while the script is paused I click on any of the quantity elements to give one of them focus and then unpause the script, the next iteration works. The _IEAction gives focus to the new quantity element that gets created and the send command works.

Why will the _IEAction not work for the next loop simply because I removed focus from it with the blur command? Or if focus is removed from the quantity element in any other way.

What I really want to do in the "do some stuff" is simply click a confirm link but that also removes the focus from the quantity element. That's why I found this problem. The script will work fine if I click on the confirm link and specifically do another _IEAction to put focus back on the quantity element but that's not always possible because in some cases I click another link that removes all the elements from formSlip so I cannot put focus back into it. The actual calculation of the _IEFormElementGetCollection($oForm, $i*2-1) command takes that into account. In my real code it's not $i*2-1 but I wanted to keep this code as simple as possible to show my problem.

Anyone have any ideas why this strange behaviour or what I can try to work around this problem considering it is not possible for me to specifically put focus back into the quantity element as the last part of my loop?

Oh, javascript is also executed on the quantity element. Again it executes some javascript in the dummy frame. It has several .js files attached to the site and I guess most of the javascript is in those. So I don't know exactly what the javascript does except what I actually see on the web page. In the quantity element it dynamically displays the total cost (quantity times the cost of the item I'm ordering).

FYI I'm very new to autoit. At first I didn't even know about ie.au3. I was trying to do all this with pixelchecksum but that became a nightmare trying to look for each grocery item based on a specific checksum. And then what if it wasn't visible on the screen because I had to use a scrollbar... A real nightmare. When I finally read about ie.au3 I thought this is perfect for what I was doing. Everything fell right into place until I hit this strange anomaly.

Since I'm new to Autoit, the fanciest debugging tool I have is msgbox. But I am somewhat familiar with HTML and javascript so I wrote a program that dumped the information from _IEBodyReadHTML into a file so I could see things like this formSlip being created on the fly and things like that. But none of that helps when I can't even put focus on an element for some weird reason.

Link to comment
Share on other sites

The first thing you need to know is if $oElement is retreived correctly each time. I am suspicious of the calculation for the index number. Try this for debug and run it in SciTE to see the output:

#include <IE.au3>

_IEErrorHandlerRegister()

; ... rest of the script

For $i = 1 To $grocData[0][0]
    _IELinkClickByIndex($oFrame, $grocData[$i][2])
    $oForm = _IEFormGetObjByName($oFrame, "formSlip")
    $oElement = _IEFormElementGetCollection($oForm, $i * 2 - 1)
    ConsoleWrite("Debug: Form element index = " & $i * 2 - 1 & ", $oElement type = " & ObjName($oElement) & _
            ", HTML = " & $oElement.outerhtml & @LF)
    _IEAction($oElement, "focus")
    Send($grocData[$i][3])
    ;do some stuff
    _IEAction($oElement, "blur");debug line 1
    Send("{PAUSE}");debug line 2
Next

:P

Corrected ".outerhtml" for ".outterhtml" per Dale. :)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

The first thing you need to know is if $oElement is retreived correctly each time. I am suspicious of the calculation for the index number. Try this for debug and run it in SciTE to see the output:

#include <IE.au3>

_IEErrorHandlerRegister()

; ... rest of the script

For $i = 1 To $grocData[0][0]
    _IELinkClickByIndex($oFrame, $grocData[$i][2])
    $oForm = _IEFormGetObjByName($oFrame, "formSlip")
    $oElement = _IEFormElementGetCollection($oForm, $i * 2 - 1)
    ConsoleWrite("Debug: Form element index = " & $i * 2 - 1 & ", $oElement type = " & ObjName($oElement) & _
            ", HTML = " & $oElement.outterhtml & @LF)
    _IEAction($oElement, "focus")
    Send($grocData[$i][3])
    ;do some stuff
    _IEAction($oElement, "blur");debug line 1
    Send("{PAUSE}");debug line 2
Next

:)

Your debug information does not seem to produce very useful results. Maybe I missed something from your instructions? Anyway, I ran 2 tests. The first is with this code which appears to work fine in the web page display:

_IEErrorHandlerRegister()
for $i = 1 to 4;$teamData[0][0]
  _IELinkClickByIndex($oFrame, $teamData[$i][2])
  $oForm = _IEFormGetObjByName ($oFrame, "formSlip")
  $oElement = _IEFormElementGetCollection($oForm, $i*2-1)
  _IEAction($oElement, "focus")
  Send($teamData[$i][3])
;  _IEAction($oElement, "blur")
;  Send("{PAUSE}")
ConsoleWrite("Debug: Form element index = " & $i * 2 - 1 & ", $oElement type = " & ObjName($oElement) & ", HTML = " & $oElement.outterhtml & @LF)
;ConsoleWrite("Debug: Form element index = " & $i * 2 - 1 & ", $oElement type = " & ObjName($oElement)  & ", name = " & $oElement.name & ", value = " & $oElement.value & @LF)
next

The second test is with the exact same code except the _IEAction($oElement, "blur") and Send("{PAUSE}") are uncommented so I can take manual control of the focus on each loop. I then ran the exact same 2 tests again with my own debug message instead of yours. The output of all four test is below.

For the 2nd and 4th test I did this:

1) For the first loop I simply press pause so that nothing has focus. You can see the 2nd iteration doesn't have a value then.

2) For the second loop I purposely click on element st32278592, then press pause and then the next iteration works.

3) For the third loop I simply press pause again and therefore the 4th quantity is also blank.

I see the value shows as 0 in the debug info but on the web page the quantity element is simply blank.

Any ideas?

To me what appeared as a random issue at first is now very clear. If I don't have focus on the quantity element, the _IEAction($oElement, "focus") for the next loop fails and therefore the next quantity gets no value.

Your output from test 1:

Debug: Form element index = 1, $oElement type = DispHTMLInputElement, HTML =

--> COM Error Encountered in orderList2.au3

----> $IEComErrorScriptline = 51

----> $IEComErrorNumberHex = 80020006

----> $IEComErrorNumber = -2147352570

----> $IEComErrorWinDescription = Unknown name.

----> $IEComErrorDescription =

----> $IEComErrorSource =

----> $IEComErrorHelpFile =

----> $IEComErrorHelpContext =

----> $IEComErrorLastDllError = 0

Debug: Form element index = 3, $oElement type = DispHTMLInputElement, HTML =

--> COM Error Encountered in orderList2.au3

----> $IEComErrorScriptline = 51

----> $IEComErrorNumberHex = 80020006

----> $IEComErrorNumber = -2147352570

----> $IEComErrorWinDescription = Unknown name.

----> $IEComErrorDescription =

----> $IEComErrorSource =

----> $IEComErrorHelpFile =

----> $IEComErrorHelpContext =

----> $IEComErrorLastDllError = 0

Debug: Form element index = 5, $oElement type = DispHTMLInputElement, HTML =

--> COM Error Encountered in orderList2.au3

----> $IEComErrorScriptline = 51

----> $IEComErrorNumberHex = 80020006

----> $IEComErrorNumber = -2147352570

----> $IEComErrorWinDescription = Unknown name.

----> $IEComErrorDescription =

----> $IEComErrorSource =

----> $IEComErrorHelpFile =

----> $IEComErrorHelpContext =

----> $IEComErrorLastDllError = 0

Debug: Form element index = 7, $oElement type = DispHTMLInputElement, HTML =

--> COM Error Encountered in orderList2.au3

----> $IEComErrorScriptline = 51

----> $IEComErrorNumberHex = 80020006

----> $IEComErrorNumber = -2147352570

----> $IEComErrorWinDescription = Unknown name.

----> $IEComErrorDescription =

----> $IEComErrorSource =

----> $IEComErrorHelpFile =

----> $IEComErrorHelpContext =

----> $IEComErrorLastDllError = 0

>Exit code: 0 Time: 13.921

Your output from test 2:

Debug: Form element index = 1, $oElement type = DispHTMLInputElement, HTML =

--> COM Error Encountered in orderList2.au3

----> $IEComErrorScriptline = 51

----> $IEComErrorNumberHex = 80020006

----> $IEComErrorNumber = -2147352570

----> $IEComErrorWinDescription = Unknown name.

----> $IEComErrorDescription =

----> $IEComErrorSource =

----> $IEComErrorHelpFile =

----> $IEComErrorHelpContext =

----> $IEComErrorLastDllError = 0

Debug: Form element index = 3, $oElement type = DispHTMLInputElement, HTML =

--> COM Error Encountered in orderList2.au3

----> $IEComErrorScriptline = 51

----> $IEComErrorNumberHex = 80020006

----> $IEComErrorNumber = -2147352570

----> $IEComErrorWinDescription = Unknown name.

----> $IEComErrorDescription =

----> $IEComErrorSource =

----> $IEComErrorHelpFile =

----> $IEComErrorHelpContext =

----> $IEComErrorLastDllError = 0

Debug: Form element index = 5, $oElement type = DispHTMLInputElement, HTML =

--> COM Error Encountered in orderList2.au3

----> $IEComErrorScriptline = 51

----> $IEComErrorNumberHex = 80020006

----> $IEComErrorNumber = -2147352570

----> $IEComErrorWinDescription = Unknown name.

----> $IEComErrorDescription =

----> $IEComErrorSource =

----> $IEComErrorHelpFile =

----> $IEComErrorHelpContext =

----> $IEComErrorLastDllError = 0

Debug: Form element index = 7, $oElement type = DispHTMLInputElement, HTML =

--> COM Error Encountered in orderList2.au3

----> $IEComErrorScriptline = 51

----> $IEComErrorNumberHex = 80020006

----> $IEComErrorNumber = -2147352570

----> $IEComErrorWinDescription = Unknown name.

----> $IEComErrorDescription =

----> $IEComErrorSource =

----> $IEComErrorHelpFile =

----> $IEComErrorHelpContext =

----> $IEComErrorLastDllError = 0

>Exit code: 0 Time: 26.999

My output from test 1:

Debug: Form element index = 1, $oElement type = DispHTMLInputElement, name = st32278592, value = 1.68

Debug: Form element index = 3, $oElement type = DispHTMLInputElement, name = st32278593, value = 2.78

Debug: Form element index = 5, $oElement type = DispHTMLInputElement, name = st32278594, value = 2.56

Debug: Form element index = 7, $oElement type = DispHTMLInputElement, name = st32278595, value = 7.41

My output from test 2:

Debug: Form element index = 1, $oElement type = DispHTMLInputElement, name = st32278592, value = 1.68

Debug: Form element index = 3, $oElement type = DispHTMLInputElement, name = st32278593, value = 0

Debug: Form element index = 5, $oElement type = DispHTMLInputElement, name = st32278594, value = 2.56

Debug: Form element index = 7, $oElement type = DispHTMLInputElement, name = st32278595, value = 0

Link to comment
Share on other sites

You have a typo: $oElement.outterhtml

$oElement.outerhtml

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

To quote the sage philosopher Homer: Doh! :P

Here is the output of 12 iterations. Today has an anomaly though ;) I thought I at least found a pattern with this problem but I see today it's not always 100%. Below is one sample test result where I took manual control of the focus. Every time I purposedly undid the blur command by putting focus back on any of the elements displayed below before unpausing, the next iteration would put a value in the element. If I simply unpaused without putting focus back on something, the next iteration left the element blank. But for some reason iteration 4 is not following that rule today. I have no idea why. It is always getting a value regardless of what I do with the previous iteration.

But that is not so important. If I could just count on the focus command working all the time then I know this logic would work OK, regardless of what other abnormalities could be happening behind the scenes.

So does the output below give you any clues to what is going wrong? I doubt it, but it should prove that it's not a simple error on my part. I hope :D

Debug: Form element index = 1, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(1,event) onkeyup='java script:parent.stk("32291994");' tabIndex=1 maxLength=8 size=3 value=1.32 name=st32291994>

Debug: Form element index = 3, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(2,event) onkeyup='java script:parent.stk("32291995");' tabIndex=2 maxLength=8 size=3 name=st32291995>

Debug: Form element index = 5, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(3,event) onkeyup='java script:parent.stk("32291996");' tabIndex=3 maxLength=8 size=3 value=12.86 name=st32291996>

Debug: Form element index = 7, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(4,event) onkeyup='java script:parent.stk("32291997");' tabIndex=4 maxLength=8 size=3 value=1.63 name=st32291997>

Debug: Form element index = 9, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(5,event) onkeyup='java script:parent.stk("32292006");' tabIndex=5 maxLength=8 size=3 name=st32292006>

Debug: Form element index = 11, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(6,event) onkeyup='java script:parent.stk("32292007");' tabIndex=6 maxLength=8 size=3 name=st32292007>

Debug: Form element index = 13, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(7,event) onkeyup='java script:parent.stk("32292002");' tabIndex=7 maxLength=8 size=3 value=2.04 name=st32292002>

Debug: Form element index = 15, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(8,event) onkeyup='java script:parent.stk("32292003");' tabIndex=8 maxLength=8 size=3 value=2.35 name=st32292003>

Debug: Form element index = 17, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(9,event) onkeyup='java script:parent.stk("32292004");' tabIndex=9 maxLength=8 size=3 name=st32292004>

Debug: Form element index = 19, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(10,event) onkeyup='java script:parent.stk("32292005");' tabIndex=10 maxLength=8 size=3 name=st32292005>

Debug: Form element index = 21, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(11,event) onkeyup='java script:parent.stk("32291998");' tabIndex=11 maxLength=8 size=3 name=st32291998>

Debug: Form element index = 23, $oElement type = DispHTMLInputElement, HTML = <INPUT class=checkStyle onkeypress=java script:tab(12,event) onkeyup='java script:parent.stk("32291999");' tabIndex=12 maxLength=8 size=3 value=2.25 name=st32291999>

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...