Link to home
Start Free TrialLog in
Avatar of Paolo Crossi
Paolo Crossi

asked on

Automating Internet Explorer via Excel VBA: manage mouse events

Hello everybody.

I usually apply a process for typing in site fields, very easy but effective.

For example, in a login mask:

IE.document.getElementsByTagName("INPUT")(0).Value = myLogin
IE.document.getElementsByTagName("INPUT")(1).Value = myPassword
IE.document.getElementsByTagName("button")(0).Click

Open in new window


But now I have bumped into a site with some peculiarities.

Exactly, I can type myId and myPassword with Vba, I can also click, but than the system gets the error

"All fields must be filled in"

as both login field and password field were not filled.

(I precise that it is an intranet site, so you cannot reach it.)

So, in analising the site architecture, I have come to the conclusion that I have to active an event, I mean a mouse click in the field, as manually I do when I access the site.

For example, the login field is as follows (consider the id is dynamic, each time is different):

<input id="in2598" class="bb-textbox" type="text" maxlength="18" value=""> 

Open in new window


But I have noticed when I manually click in the field, the html code changes in:

<input id="in2598" class="bb-textbox bb-textbox-focus" type="text" maxlength="18" value=""> 

Open in new window



Consider it is also a .zul site and something - to me it is not clear what - could be defined by ZKoss Javascript library.

So far, I have tried something like this, but unsuccesfully.


Dim HTMLdoc As HTMLDocument
Set HTMLdoc = IE.document

Dim evtclick As Object
Set evtclick = HTMLdoc.createEvent("HTMLEvents")

evtclick.initEvent "HTMLEvents", True, False

IE.document.getElementsByTagName("INPUT")(0).Value = myLogin
IE.document.getElementsByTagName("INPUT")(0).Value = dispatchEvent evtclick

Open in new window


Tell me if I have to get further details.
Avatar of Norie
Norie

Paolo

Have you tried submitting the login form rather than clicking the button?
IE.document.forms(0).submit

Open in new window

Avatar of Paolo Crossi

ASKER

Do you mean as follows?

IE.document.getElementsByTagName("INPUT")(0).Value = myId
IE.document.getElementsByTagName("INPUT")(1).Value = myCode
IE.document.forms(0).submit

Open in new window


"Run Time Error '424' object required" on the following row:

IE.document.forms(0).submit

Open in new window

Paolo

That is what I mean but if that's the error message you are getting it would appear the page doesn't have any forms on it, well not 'standard' forms anyway.

You mention a  ZKoss Javascript library, do you have a link for that?
I can try adding elements.

In the page html I can read:

<div id="k4UU_" class="bb-temp"></div>
<script class="bb-runonce" type="text/javascript">zk.pi=1;zkmx(
[0,'k4UU_',{dt:'z_1s50',cu:'/EXAMP;jsessionid=FC8F3D4E6DDC855F7FD11349F6AFC88D',uu:'/EXAMP/zkau;jsessionid=FC8F3D4E6DDC855F7FD11349F6AFC88D',ru:'/zulpages/login.zul'},[
['zul.wnd.Window','k4UU0',{$onOK:true,$$onSize:false,$$onMaximize:false,$$onOpen:false,$$onMinimize:false,$$onZIndex:false,$onClose:true,$$onMove:false,width:'350px',title:'SYSTEM MANAGEMENT,position:'center',border:'normal',mode:'modal'},[
['zul.box.Box','k4UU1',{prolog:'\n\t'},[
['zul.grid.Grid','k4UU2',{$$onRender:true,$$onDataLoading:true,$$onInnerWidth:false,$$onPageSize:true,$$onScrollPos:false,width:'330px',_topPad:0,emptyMessage:null,_totalSize:2,_offset:0},[
['zul.grid.Rows','k4UU3',{_offset:0,visibleItemCount:2},[
['zul.grid.Row','k4UU4',{width:'330px'},[
['zul.wgt.Label','k4UU5',{id:'userLabel',value:'User'},[]],
['zul.wgt.Div','k4UU6',{align:'center'},[
['zul.inp.Textbox','k4UU7',{id:'user',$$onError:false,$$onChange:false,prolog:'\n\t\t\t\t\t\t',maxlength:18},[]]]]]],
['zul.grid.Row','k4UU8',{width:'330px'},[
['zul.wgt.Label','k4UU9',{id:'passwordLabel',value:'Password'},[]],
['zul.wgt.Div','k4UUa',{align:'center'},[
['zul.inp.Textbox','k4UUb',{id:'password',prolog:'\n\t\t\t\t\t\t',maxlength:25,type:'password'},[]]]]]],
['zul.grid.Row','k4UUc',{id:'NewPasswordRow',visible:false,width:'330px'},[
['zul.wgt.Label','k4UUd',{id:'NewPasswordLabel',value:'New password'},[]],
['zul.wgt.Div','k4UUe',{align:'center'},[
['zul.inp.Textbox','k4UUf',{id:'newPassword',prolog:'\n\t\t\t\t\t\t',maxlength:25,type:'password'},[]]]]]],
['zul.grid.Row','k4UUg',{id:'confirmPasswordRow',visible:false,width:'330px'},[
['zul.wgt.Label','k4UUh',{id:'confirmPasswordLabel',value:'Confirm password'},[]],
['zul.wgt.Div','k4UUi',{align:'center'},[
['zul.inp.Textbox','k4UUj',{id:'confirmPassword',$onChange:true,prolog:'\n\t\t\t\t\t\t',maxlength:25,type:'password'},[]]]]]],
['zul.grid.Row','k4UUk',{visible:false,width:'330px'},[
['zul.wgt.Label','k4UUl',{id:'languageLabel',value:'Language'},[]],
['zul.wgt.Div','k4UUm',{align:'center'},[
['zul.inp.Combobox','k4UUn',{id:'language',$$onSelect:false,$$onError:false,$$onChange:false,$onChange:true,prolog:'\n\t\t\t\t\t\t'},[
['zul.inp.Comboitem','k4UUo',{},[]],
['zul.inp.Comboitem','k4UUp',{},[]]]]]]]]]]]],
['zul.wgt.Separator','k4UUq',{height:'10px',orient:'vertical'},[]],
['zul.wgt.Div','k4UUr',{align:'center'},[
['zul.wgt.Button','k4UUs',{id:'logInBtn',$onClick:true,prolog:'\n\t\t\t',label:'Enter'},[]],
['zul.wgt.Button','k4UUt',{id:'changePasswordBtn',visible:false,$onClick:true,prolog:'\n\t\t\t',label:'Change password'},[]]]],
['zul.wgt.Div','k4UUu',{align:'center'},[
['zul.wgt.Label','k4UUv',{id:'msg',style:'color:red;font-weight:bold',prolog:'\n\t\t\t'},[]]]]],'vertical']]]]]);
</script>

Open in new window


I'm trying to decrypt the code by using the following site:

https://www.zkoss.org/zkdemo/event
instead evtClick, use evtBlur or evtChange
I could try, but I am not sure how to set the event (it is the first time I process in this way).

Dim HTMLdoc As HTMLDocument
Set HTMLdoc = IE.document

Dim evtChange As Object
Set evtChange = HTMLdoc.createEvent("HTMLEvents")

evtChange.initEvent "change", True, False


Dim Storesel As HTMLSelectElement

    Set Storesel = HTMLdoc.getElementsByTagName("input")(0)
    Storesel.Focus
    Storesel.Value = myId
    Storesel.dispatchEvent evtChange

Open in new window


It does not seem correct, becuase of the "Run-time error 438: Object doesn't support this property" on the following line:

Storesel.Value = myId

Open in new window

Is Storesel a dropdown/combobox/listbox?
It does not seem correct, becuase of the "Run-time error 438: Object doesn't support this property" on the following line:

so now, you don't know how to set the value of the textbox ?
Consider this just an idea (I have done some arrangements).
login-mask.png
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm testing this:

IE.document.getElementsByTagName("INPUT")(0).Focus
IE.document.getElementsByTagName("INPUT")(0).Value = myLogin
IE.document.getElementsByTagName("INPUT")(0).Blur

IE.document.getElementsByTagName("INPUT")(1).Focus
IE.document.getElementsByTagName("INPUT")(1).Value = myPassword
IE.document.getElementsByTagName("INPUT")(1).Blur

IE.document.getElementsByTagName("button")(0).Click

Open in new window


and it seems to work fine.

I will confirm you as soon as I finish the tests.
I confirm: the following is ok:
IE.document.getElementsByTagName("INPUT")(0).Focus
IE.document.getElementsByTagName("INPUT")(0).Value = myLogin
IE.document.getElementsByTagName("INPUT")(0).Blur

IE.document.getElementsByTagName("INPUT")(1).Focus
IE.document.getElementsByTagName("INPUT")(1).Value = myPassword
IE.document.getElementsByTagName("INPUT")(1).Blur

IE.document.getElementsByTagName("button")(0).Click

Open in new window


The second one doesn't work for me:
IE.document.getElementsByTagName("INPUT")(0).Value = myLogin
IE.document.getElementsByTagName("INPUT")(1).Value = myPassword

Dim evt
Set evt = doc.createEvent("HTMLEvents")
evt.initEvent "change", False, True
e.dispatchEvent evt

IE.document.getElementsByTagName("button")(0).Click

Open in new window


with error 404 object required on row:
e.dispatchEvent evt

Open in new window


Anyway, for me is enough to go further: thank you.
Able of clicking in the field as manually happens.