How to Fire a KeyPress Event in a IE Field?

Started by mcjathan, June 15, 2017, 02:24:00 PM

Previous topic - Next topic

mcjathan

Greetings,

I've been updating some of our Winbatch scripts to run on the newer Windows and newer Internet Explorer (IE).  This forum thread titled "Changes to Internet Explorer DOM" has been very helpful:

     http://forum.winbatch.com/index.php?topic=923.0

However, I've run into a roadblock that I need some help with.  There is a field that I'm able to fill with the desired value.  However, to proceed to the next web-page, there is a "Next" button that must be clicked but this "Next" button is disabled until the field detects a keypress (the same field I'm loading with a value).  Simply loading the field with my desired value doesn't fire a keypress.

A screenshot of the web-page is attached at the bottom of this posting.

Here is the HTML code from that field:

Quote<input name="question.answer" value="" id="securityAnswer" tabindex="1" class="inputm textborder" maxlength="40" onkeypress="return keyPressEvent(event)" aria-required="true" type="password">

Here's my Winbatch code that successfully fills the field with my value and attempts to click on the field so I can send keys to the field:

Quotemyinput = all.item("securityAnswer")
objEvent = browserdoc.createEvent("HTMLEvents")

myinput.value = @tmp_Array[0, @AnswerCol]   ; Enter security question's answer

objEvent.initEvent("click", @true, @true)
myinput.dispatchEvent(objEvent)

I've tried "click" and "focus" but I can't select the field (so I can send keystrokes like a left or right arrow).  An even better solution would be if I could programmatically fire a key event.

Any suggestions?

td

Have you tried appending a @LF or @CRLF to your input string value?
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

mcjathan

I hadn't thought of trying to append @LF or @CRLF.  Unfortunately, however, this didn't work   :(

Right after entering the value plus the @LF or @CRLF into the field, I had my Winbatch script exit.  Then, manually, I clicked in the field and arrowed left (or right).  The "Next" button enabled instantly once I arrowed.  Then, continuing manually, I clicked on the "Next" button and everything worked properly.  So close...

Please, any other suggestions?

td

The @lf or @crlf was a shot in the dark so it is not surprising that it didn't work.  It could be something simple that we are just overlooking. When you state that you have tried sending a "click" event, does that mean that you have tried sending a "click" event to the myinput object or to the arrow object?
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor


mcjathan

Quote from: td on June 16, 2017, 11:07:05 AM
The @lf or @crlf was a shot in the dark so it is not surprising that it didn't work.  It could be something simple that we are just overlooking. When you state that you have tried sending a "click" event, does that mean that you have tried sending a "click" event to the myinput object or to the arrow object?
I've tried both clicking and focusing on the myinput object.  I'm not sure what you mean by the arrow object.

mcjathan

Quote from: JTaylor on June 16, 2017, 01:11:42 PM
Have you tried enabling the button?

Jim
Hi Jim,  Thanks for jumping in!
That hadn't occurred to me. 

Here's the old code that locates and clicks on this button:

Quoteinputlist = browserdoc.GetElementsByTagName("A")
For i = 0 To inputlist.length-1
   thisinput = inputlist.item(i)
   temp2 = thisinput.id
   If temp2 == "continuebutton" 
      thisinput.click
      ObjectClose(thisinput)
      Break
   EndIf
Next

Keeping in mind that this code needs to be modernized, I would be looking to add code like this:

QuoteobjEvent.initEvent("click", @true, @true)
Anchor.dispatchEvent(objEvent)

But perhaps look for a "enable" instead of a "click"?  I tried Googling for this but couldn't find anything.

Have you enabled a button like this before?

JTaylor

Let me see if I can track something down.  If you can find the keypress event source code it should have what you need since they have obviously disabled it and then enable it on a keypress.   If you do that then the plan to click should work.

Jim

JTaylor

 If the following works then just do your click after, as you intended.

Jim

Code (winbatch) Select

ybrowser = your browser object...

    button = ybrowser.document.GetElementById("button_id")

    button.disabled = !button.disabled





mcjathan

Quote from: JTaylor on June 16, 2017, 06:39:58 PM
If the following works then just do your click after, as you intended.

Jim

Code (winbatch) Select

ybrowser = your browser object...

    button = ybrowser.document.GetElementById("button_id")

    button.disabled = !button.disabled


Unfortunately, this didn't work.

Any other suggestions?

JTaylor


mcjathan

Quote from: JTaylor on June 20, 2017, 05:58:05 PM
The button doesn't become active?
Unfortunately, no, it doesn't become active.

Here's my actual Winbatch code:
Code (winbatch) Select
button = browserdoc.GetElementById("continuebutton")
button.disabled = !button.disabled

This Winbatch code executes without error, but the button simply stays disabled.

I don't know if this is helpful, but here's the HTML code for this button initially (while disabled) and after I manually caused a keyevent.  Here's the button's HTML while disabled:
Code (HTML) Select
<a class="btn btnOff " id="continuebutton" href="javascript:CallContinue();" tabindex="1"><span>Next </span></a>

Here's the button's HTML code after it's been manually enabled:
Code (HTML) Select
<a class="btn  " id="continuebutton" href="javascript:CallContinue();" tabindex="1"><span>Next
</span></a>


The button's class changes. Is that significant in regards to coming up with a solution?

Also, I see that when the button is pressed (after enabling), the javascript function "CallContinue() is executed.  Could we simple execute this function directly?

JTaylor

Yes.   The Class is changing it back and keeping it disabled.

Just change the Class to what they show.  You may still need to enable the button.  Not seeing the CSS I couldn't say either way.

Crazy busy right now so haven't tested but something along the lines of the following might do what you need.   

button.className = "btn  "
button.disabled = !button.disabled

Jim

mcjathan

That did the trick, Jim!
Code (winbatch) Select
button.className = "btn  "

I needed to remove this line:
Code (winbatch) Select
button.disabled = !button.disabled

Thank you!

JTaylor

Excellent.   Sounds like the CSS handled both sides of it.

Jim

td

This thread is a bit embarrassing.  I created a new Web page a few days ago that uses javascript to change CSS styles of a page element to enable and disable that element.  Didn't make the connection... 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade