Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Automation IE7 Navigation and Scripting with Tabs


  • This topic is locked This topic is locked
214 replies to this topic

Poll: Do you find this group pf wrapper functions for IE Automation Usefull (35 member(s) have cast votes)

Do you find this group pf wrapper functions for IE Automation Usefull

  1. no you suck (3 votes [8.33%])

    Percentage of vote: 8.33%

  2. need better Documentation (10 votes [27.78%])

    Percentage of vote: 27.78%

  3. What is DHTML i dont understand how this helps (4 votes [11.11%])

    Percentage of vote: 11.11%

  4. I use Firefox this is a stupid idea (10 votes [27.78%])

    Percentage of vote: 27.78%

  5. I know DHTML this is a reasonably good AHK adaption (2 votes [5.56%])

    Percentage of vote: 5.56%

  6. This is the best DHTML too in the AHK Forum (7 votes [19.44%])

    Percentage of vote: 19.44%

Vote
tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007
please post your code
Never lose.
WIN or LEARN.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007
cross frame script support added
Never lose.
WIN or LEARN.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007
For those selecting this is a stupid idea when mozilla makes a com interface avail i will agree
Never lose.
WIN or LEARN.

neXt
  • Members
  • 549 posts
  • Last active: May 20 2015 02:38 AM
  • Joined: 18 Mar 2007
It is definitely useful, but i would appreciate an example per function.
btw, where is the list of all available functions?

CannedCheese
  • Members
  • 120 posts
  • Last active: Sep 24 2014 04:31 PM
  • Joined: 21 May 2008
Just wanted to add that I'm very grateful for this code, I've already made heavy use of it (still need to update my functions) in automating IE7 background tasks. At some point I plan to post some examples but currently I am only using it to automate online database tasks at private sites. Cant wait for more examples/documentation.

rani
  • Members
  • 217 posts
  • Last active: Jul 21 2016 12:53 PM
  • Joined: 18 Mar 2008
all commands work
but the execScript, to execute functions from the Navigate Doc
is not executing
what is the object to execute function ?
how I can simulate with COM_Invoke to run JS function+it's params ?

here is some sample code:
;
;#Include CoHelper.ahk
#Include COM.ahk
main:
gosub,IEconnect ;

qryString := "My First Query" ;
gosub,httpRequest ;
gosub,httpResponse ;

return

IEconnect:

COM_Init() ;

oIE := COM_CreateObject("InternetExplorer.Application")
COM_Invoke(oIE, "Visible=", "True") ;"False" ;"True" ;
URL := "http://LocalHost/XMLHttpRequest.htm"
COM_Invoke(oIE, "Navigate", URL)
oDoc := COM_Invoke(oIE,"Document")
oWin :=COM_Invoke(oDoc, "parentWindow")
msgbox , active IE url: %URL%
gosub,ReadyState
;oPhpReq = Invoke(oIE,"PhpReq")

return

httpRequest:
JSexec := "httpRequest(" . chr(34) . qryString . chr(34) . ")" ;
JSexec := "alert('JS exec alert')"
msgBox, JS: %JSexec%
COM_Invoke(oIE,"execScript",JSexec) ; <------------- not working
o := COM_Invoke(oDoc,"getElementById","cmdHttp")
return

httpResponse:
o := COM_Invoke(oDoc,"getElementById","msg")
text := COM_Invoke(o,"innerHTML")
o := COM_Invoke(oDoc,"getElementById","job")
count := COM_Invoke(o,"innerHTML")
msgBox , text %text% count %job%

return


ReadyState:

Loop
{
state:= COM_Invoke(oIE, "readyState")
msgbox, URL ready state %URL% state %state%
If state <> 4
Sleep, 500
Else
Break
}
return

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007

It is definitely useful, but i would appreciate an example per function.
btw, where is the list of all available functions?

in the front page i will poste here again but will not ever update this copy of the functions


;~ ******************************************************************************************
;~ ******************************************************************************************
;~ IE 7 functions
;~ ******************************************************************************************
;~ ******************************************************************************************
;~ function list
;~ IE7_Get(title,url="http") gets iwebrowser interface pointer and returns automation dhtml window obj ie parentWindow used itn the below functions
;~ IE7_Navigate(parentWindow,url)  Navigate and wait for page Load to complete in same frame
;~ IE7_ExecuteJS(parentWindow, JS_to_Inject, VarNames_to_Return="") Full insertion of javascript or execution or retrieval of new or existing fucntions or variables
;~ IE7_readyState(parentWindow) wait for page Load to complete
;~ IE7_Quit(parentWindow) Closes Window
;~ IE7_New(url) Loads a new window even if one does not currently exist and wait for page Load to complete in same frame
;~ IE7_Click_Text(parentWindow,innerHTML)Clicks a link byt text in the inner html of the a tag and wait for page Load to complete in same frame
;~ IE7_Click_Element(parentWindow,ID1=0) ; clicks first element it finds to match returns error if not found will also trigger readystate
;~ IE7_Button_click(parentWindow,value)Click a button based on the text of the button and wait for page Load to complete in same frame
;~ IE7_Get_DOM(parentWindow,ID1)retreive a dom object by index of all collection id or name attribute
;~ IE7_Set_DOM(parentWindow,ID1,val="innerHTML") set  a dom object by index of all collection id or name attribute
;~ ******************************************************************************************
;~ ******************************************************************************************
IE7_Get(title,url="http")
{
;~    title is required and is not case sensitive
;~    url is optional and since . exists in all url schemas it will allow un entered url to default to first found
;~  loop thru open windows for a match
   Loop, %   COM_Invoke(psw := COM_Invoke(psh:=COM_CreateObject("Shell.Application"), "Windows"), "Count")
   {
      If ( InStr(LocationName:=COM_Invoke(pwb:=COM_Invoke(psw, "Item", A_Index-1), "LocationName"),title,0)) && (InStr(taburl:=COM_Invoke(pwb, "LocationURL"),url,0) )
      {
         parentWindow:=COM_Invoke(Document:=COM_Invoke(pwb, "Document"), "parentWindow")
         COM_Release(pwb),COM_Release(Document)
         Break
      }
         COM_Release(pwb),COM_Release(Document)
   }
   COM_Release(psw),COM_Release(psh) ; release the objects each loop itineration
   if parentWindow is integer
   {
      return parentWindow
   }
   else
   {
      Return A_LastError    
   }
}

;~ Navigate the given window
IE7_Navigate(parentWindow,url) ; begins navigation process and holds execution till page has loaded fully
{
;~    parent windw is a Dom window object
   if parentWindow is integer
   {   
      result:=COM_Invoke(parentWindow, "Navigate",url) ;execute the navigation returns SOK 
      IE7_readyState(parentWindow) ;Holds till document onload event is ready
      Return result 
   }
}


IE7_ExecuteJS(parentWindow, JS_to_Inject, VarNames_to_Return="") ;execute some js on a tab returns comma delimited list of vars if specified
{
   if parentWindow is integer
   {

      If JS_to_Inject
         COM_Invoke(parentWindow, "execScript",JS_to_Inject)
      If VarNames_to_Return
      {
         StringSplit, Vars_, VarNames_to_Return, `,
         Loop, %Vars_0%
            Ret .= COM_Invoke(parentWindow,Vars_%A_Index%) . ","
         StringTrimRight, Ret, Ret, 1
         Return Ret
      }
         Else
         Return 0
   }
   Else
      Return error
}
IE7_readyState(parentWindow) ;pauses till there is no loading activity returns DOM window handle
{
   
   
   if parentWindow is integer
   {
      loop 120{ ;limit to 60 seconds
         Sleep, 500
         rdy:=IE7_ExecuteJS(parentWindow, "var rdy=document.readyState","rdy") ; best method
         If (rdy = "complete") ;Better to use the document readystate more consistent page load results
            break
         }
      Return 0
   }
   else
      Return A_LastError   
   
}
IE7_Quit(parentWindow) ;Close a DOM window handle
{
   COM_Invoke(parentWindow, "execScript","window.opener='top';window.close();")
}

IE7_New(url) ;only if at least one IE window already exists return parentWindow js accessable object returns DOM window handle
{
   run http://Google.com
   winwait,Google
   parentWindow:=IE7_Get("Google")
   IE7_Navigate(parentWindow,url)
   return parentWindow ; object handle
}


IE7_Click_Text(parentWindow,innerHTML) ; searches a tags and clicks first text it finds to match returns error if not found will also trigger readystate
{
   js=
   (
      var links=document.links;
      var count=links.length;
   )
   
   count1 := IE7_ExecuteJS(parentWindow, js, "count")
   loop, % count1
   {
   
      js=
      (
         var text=document.links(%A_Index%-1).innerHTML;
      )
      text1 := IE7_ExecuteJS(parentWindow, js, "text")
      IfNotInString,text1,%innerHTML%
      Continue ;skip the rest and go to next loop
      js=
      (
         document.links(%A_Index%-1).click();
      )
      IE7_ExecuteJS(parentWindow, js)     
      Break
   }
IE7_readyState(parentWindow) ; wait for page change to finish
Return
}

IE7_Click_Element(parentWindow,ID1=0) ; clicks first element it finds to match returns error if not found will also trigger readystate
{
   clickme=
   (
     
   if (isNaN("%ID1%")){ // determins if an index or id is entered
         document.all("%ID1%").click();
      }else{ // must be an index
         document.all(%ID1%).click();
      }
     
   )
   
   COM_Invoke(parentWindow, "execScript",clickme)
IE7_readyState(parentWindow) ; wait for page change to finish
Return
}

IE7_Button_click(parentWindow,value) ;Searches input tags for type button then compares the value with the sought text if found clicks returns error if not found will also trigger readystate
{
   button_click=
   (
   var i=0;
   while (i<document.all.length)
   {
      var tag=document.all(i).tagName;
      if (tag.toLowerCase()=='input')
      {
         var typ=document.all(i).type;
         if (typ.toLowerCase()=='button' || typ.toLowerCase()=='submit' || typ.toLowerCase()=='reset')
         {
            var myVal=document.all(i).value;
            var theVal='%value%';
            if (myVal.toLowerCase()==theVal.toLowerCase())
            {
               document.all(i).click(); //click the button
               tag=undefined;
               typ=undefined;
               break; // end the loop
            }
         }
      }
      i=i+1;
   }

)
IE7_ExecuteJS(parentWindow, button_click)
IE7_readyState(parentWindow) ; wait for page change to finish
Return
}

;~ IE7_Get_DOM(parentWindow,ID,val="innerHTML",method="ID" )
;~ parentWindow assigned by IE7_Get(title,url="http") and must be an integer that represents a dom window handle
;~ ID will be an element id, index of all
IE7_Get_DOM(parentWindow,ID1) ;gets DOM object by ID Tag and returns string or error
{
   get_=
   (
   if (isNaN("%ID1%")){ // determins if an index or id is entered
         var tag=document.all("%ID1%").tagName;
         var i=1;
      }else{ // must be an index
         var tag=document.all(%ID1%).tagName;
         var i=2;
      }
   var tag1=tag.toLowerCase();
   
   

   switch(tag1)
      {
      case "input":
        var t=1;
        break;   
      case "textarea":
        var t=1;
        break;   
      case "select":
        var t=1;
        break;
      default:
        var t=2;
      }
   if(i==1 && t==1)
      var myVal=document.all("%ID1%").value;
   else if(i==2 && t==1)
      var myVal=document.all(%ID1%).value;
   else if(i==1 && t==2)
      var myVal=document.all("%ID1%").innerHTML;
   else if(i==2 && t==2)
      var myVal=document.all(%ID1%).innerHTML;
     
        var t=0;
        var i=0;
        var tag1=0;
        var tag=0;
   )
   myVal:=IE7_ExecuteJS(parentWindow, get_,"myVal")
   IE7_ExecuteJS(parentWindow, "var myVal='undefined';")
   Return myVal
}

IE7_Set_DOM(parentWindow,ID1,val="innerHTML") ;gets DOM object by ID Tag and returns string or error
{
   set_=
   (
   if (isNaN("%ID1%")){ // determins if an index or id is entered
         var tag=document.all("%ID1%").tagName;
         var i=1;
      }else{ // must be an index
         var tag=document.all(%ID1%).tagName;
         var i=2;
      }
   var tag1=tag.toLowerCase();
   
   

   switch(tag1)
      {
      case "input":
        var t=1;
        break;   
      case "textarea":
        var t=1;
        break;   
      case "select":
        var t=1;
        break;
      default:
        var t=2;
      }
   if(i==1 && t==1)
      document.all("%ID1%").value='%val%';
   else if(i==2 && t==1)
      document.all(%ID1%).value='%val%';
   else if(i==1 && t==2)
      document.all("%ID1%").innerHTML='%val%';
   else if(i==2 && t==2)
      document.all(%ID1%).innerHTML='%val%';
        var t=0;
        var i=0;
        var tag1=0;
        var tag=0;
   )
   IE7_ExecuteJS(parentWindow, set_)
   Return
}
IE7_SetFrame_Form_DOM(parentWindow,FRM,FOM,ID1,val=0) ;gets DOM object by ID Tag and returns string or error
{
   if FRM is integer
      ahkframe:="document.frames(" . FRM . ")"
   Else
      ahkframe:="document.frames('" . FRM . "')"
   if FOM is integer
      ahkform:=ahkframe . ".document.forms(" . FOM . ")"
   Else
      ahkform:=ahkframe . ".document.forms('" . FOM . "')"
   if ID1 is integer
      ahkele:=ahkform . ".elements(" . ID1 . ")"
   Else
      ahkele:=ahkform . ".elements('" . ID1 . "')"
   
   tagName:=IE7_ExecuteJS(parentWindow, "var tagName=" . ahkele . ".tagName;" , "tagName")
   teststring:="inputselecttextarea"
   IfInString,teststring,%tagName%
      domString := ahkele . ".value"
   Else
      domString := ahkele . ".innerHTML"
   if val
      Return IE7_ExecuteJS(parentWindow, "var oDom=" . domString . "='" . val . "';","oDom")
   Else
      Return IE7_ExecuteJS(parentWindow, "var oDom=" . domString . ";","oDom")
   Return
}


IE7_SetFrame_DOM(parentWindow,FRM,ID1,val=0) ;gets DOM object by ID Tag and returns string or error
{
   if FRM is integer
      ahkframe:="document.frames(" . FRM . ")"
   Else
      ahkframe:="document.frames('" . FRM . "')"
   if ID1 is integer
      ahkele:=ahkframe . ".document.all(" . ID1 . ")"
   Else
      ahkele:=ahkframe . ".document.all('" . ID1 . "')"
   
;~    tagName:=IE7_ExecuteJS(parentWindow, "var tagName=" . ahkele . ".sourceIndex;" , "tagName")
;~    MsgBox % tagName
   tagName:=IE7_ExecuteJS(parentWindow, "var tagName=" . ahkele . ".tagName;" , "tagName")
   teststring:="inputselecttextarea"
   IfInString,teststring,%tagName%
      domString := ahkele . ".value"
   Else
      domString := ahkele . ".innerHTML"
   if val
      Return IE7_ExecuteJS(parentWindow, "var oDom=" . domString . "='" . val . "';","oDom")
   Else
      Return IE7_ExecuteJS(parentWindow, "var oDom=" . domString . ";","oDom")
   Return
}

there are examples posted
i will once again re post but i cant emphasize enuff the importance or reading thru the entirety of this thread there is alot there
COM_Init()
run iexplore.exe http://www.autohotkey.com/
WinWait,AutoHotkey,,5
sleep,1000
myPageHandle:=IE7_Get("autohotkey")
;~ MsgBox % myPageHandle
IE7_readyState(myPageHandle) ;  waits for onload event of a webpage even if an onload isnt fired
IE7_Click_Text(myPageHandle,"forum") ; cycles thru the links collection and clicksthe first text match it finds then waits for the page load to complete

IE7_Click_Text(myPageHandle,"search")
IE7_Set_DOM(myPageHandle,"search_keywords","PLEASE HELP!! why is my thingy not working as expected?") ;example of filling out a form
IE7_Button_click(myPageHandle,"search")
IE7_Click_Text(myPageHandle,"PLEASE HELP!! why is my thingy not working as expected?")
MsgBox, 262192, Meet the Robinsons, Please Read the first Post before asking me any questions, 5
IE7_Click_Text(myPageHandle,"search")
IE7_Set_DOM(myPageHandle,"search_keywords","Automation IE7 Navigation and Scripting with Tabs") ;example of filling out a form
IE7_Button_click(myPageHandle,"search") ; click a button labeled search and wait for the page to load
IE7_Click_Text(myPageHandle,"Wait for page to load before execute next command")
MsgBox, 262192, Automation IE7 Navigation and Scripting with Tabs, 5
COM_Term()
ExitApp





@ell
first of all dont take this wrong but
use code tags
second
execScript is a method of a window object
so should be used on
oWin not oIE
incorrect
COM_Invoke(oIE,"execScript",JSexec)
correct
COM_Invoke(oWin,"execScript",JSexec)

otherwise very good looking script
Never lose.
WIN or LEARN.

neXt
  • Members
  • 549 posts
  • Last active: May 20 2015 02:38 AM
  • Joined: 18 Mar 2007
I'm just saying that it's a good idea to organize your thread rather than make people read every page of it.

rani
  • Members
  • 217 posts
  • Last active: Jul 21 2016 12:53 PM
  • Joined: 18 Mar 2008
Hi Tank

1.
I found the problem on the above execScript, thanks to your advice:

the Objects hirarchy to parentWindow are:
WebBrowser.Document.parentWindow

and in our case:
oIE.oDoc.oWin

the mistake was that I wrote oWin directly under the oIE(obj WebBrowser)

now both JS functions work correctly !

2. how I can iterate on all DOM components, and filter on let say 'INPUT' tag's in a specific web Page, and get/set value's for specific elements ?

any little sample code in AHK will be appreciated.

3. [code=auto:0] tag tank:
immediatedly after my post I understand my mistake.
now I know how to send code sample.


rgrds
Ell

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007

Hi Tank

1.
I found the problem on the above execScript, thanks to your advice:

the Objects hirarchy to parentWindow are:
WebBrowser.Document.parentWindow

and in our case:
oIE.oDoc.oWin

I simply could not find it in myself to in such an eloquent way illistrate that
thank you that alone will likely help folks out a bunch

in general the all collection ca be itenerated the following ways
document.all(n)
wehre n is an integer usually i use a_index in a loop for this
document.all('name or id attribute in single quote')
by default all will return the first item matching this criteria you can further naro down such as the case of using a name on radio buttons
document.all('name or id attribute in single quote')(n)

document.all('tagName')
will return a collection of all items in the source with that tag name
most ofteh you would do something like this

var x=document.all('tagName');
x[any of the abovename or id attribute or index of this collection here]
example

var x=document.all('input');
x[0].value='thisvalue';
the above would returnx as a collection of all input tags
this ignores select(dropdown menus)
and then sets the first input element found to thisvalue

hope this helps a bit
Never lose.
WIN or LEARN.

rani
  • Members
  • 217 posts
  • Last active: Jul 21 2016 12:53 PM
  • Joined: 18 Mar 2008

var x=document.all('input');
x[0].value='thisvalue';


how is the syntax to do it with AHK ?:
means

1. find the webpage oject (it's document instance or win Handle)

2. run on the document.all components


rgrds
Ell

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007
i am really surprised you dont get this ell
given that you converted some vb automation script to com

so here goes
firstly there is the obvious answer execute it as is as javascript
as far i guess your looking for a com approach
; note that unfortunatly this method is case sensitivew which is why i perfer the js approach
COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(oIE,"Document"),"all","input") ,"Item",0),"value="),"thisvalue")

Never lose.
WIN or LEARN.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007

I'm just saying that it's a good idea to organize your thread rather than make people read every page of it.

try to understand this thread for me is done less than 10 minutes at a time and i rarely have any time to devote to it
in most cases it relies on a firm understanding of javascript and is designed for individuals comming to ahk with a firm background using DHTML
I just dont have the time to devote would you rather i didnt share at all
i have linked out to the sites that gave me all the back ground info to create them in the first place so conceivably someone could use this to understand and use the examples i have given to create there own documentation


Understand i do very much intend to add more documentation and examples for novice users but time is not conducive to this any time soon
Never lose.
WIN or LEARN.

rani
  • Members
  • 217 posts
  • Last active: Jul 21 2016 12:53 PM
  • Joined: 18 Mar 2008

firstly there is the obvious answer execute it as is as javascript
as far i guess your looking for a com approach
COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(oIE,"Document"),"all","input") ,"Item",0),"value="),"thisvalue")


tank ,
pls note that I need to execute the javascript from AHK
how I can use javascript commands (as is)
from AHK ?
means:
build javascript code, maintain it's instance,
and run it+it's enviromental var's ?
I think I need some COM_Invoke commands or else ?

rgrds
Ell

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: Oct 13 2016 01:04 AM
  • Joined: 21 Dec 2007
so confused but ok
one more time
var x=document.all('input');
x[0].value='thisvalue';
is the js example
you clearly know how to execute js per your previous post
I am a little lost on what you dont get

the js object variable x has the follwoing lifetime, and can be called and used untill the page is navigated or refreshed
in other words you can call it with the executescript over and over if you like
even between script calls
the variable x is stored in the rendered page displayed on your screen not in ahk
js_Inputs=
(
var x=document.all('input');
x[0].value='thisvalue';
)
COM_Invoke(oWin,"execScript",js_Inputs)
so then lets read those values
; set a js value using he previously created js object x
COM_Invoke(oWin,"execScript","var thisjsval=x[0].value;")
;use an ahk message box to display the value
msgbox % COM_Invoke(oWin,"thisjsval")
I am really not sure how much depth here but if you cant figure it out from here then i suggest giving whatever project you are undertaking to someone
If you take the time to read in detail the posts and functions you will see this is used extensively thru out my scripts
Never lose.
WIN or LEARN.