The problem of Dom

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
xuezhe
Posts: 91
Joined: 06 Jan 2016, 11:02

The problem of Dom

14 Nov 2017, 03:30

It can't work after changing it to function.

works well:

Code: Select all

window := ComObjCreate("InternetExplorer.Application")  
window.Visible := 1   
window.Navigate("https://www.baidu.com")  

Milliseconds:="600"
StartTime := A_TickCount
while window.busy
{
	Sleep 50
	ElapsedTime := A_TickCount - StartTime
	if(ElapsedTime>Milliseconds)
	         MsgBox   "超时1    "  ElapsedTime
}
Loop {
	   Sleep 50
	    ElapsedTime := A_TickCount - StartTime
	    if(ElapsedTime>Milliseconds)
	         MsgBox  "超时2     "  ElapsedTime
    	     element :=window.document.getElementById("kw")
} Until IsObject(element)

doesn't work:

Code: Select all

window := ComObjCreate("InternetExplorer.Application")  
window.Visible := 1   
window.Navigate("https://www.baidu.com")  

domElementObject:=window.document.getElementById("kw")
MsgBox  DomElementReady(domElementObject)

DomElementReady(domElementObject,Milliseconds:="5000"){
	global window
	StartTime := A_TickCount
	while window.busy
	{
		Sleep 50
		ElapsedTime := A_TickCount - StartTime
		if(ElapsedTime>Milliseconds)
		         Return -1  ;超时,返回为-1
	}
 	Loop {
		   Sleep 50
		    ElapsedTime := A_TickCount - StartTime
		    if(ElapsedTime>Milliseconds)
		         Return -1  ;超时,返回为-1
              	     element :=domElementObject
	} Until IsObject(element)
	
	Return 1
}
teadrinker
Posts: 4391
Joined: 29 Mar 2015, 09:41
Contact:

Re: The problem of Dom

14 Nov 2017, 05:28

The problem is that you try to get domElementObject before the document is loaded.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: The problem of Dom

14 Nov 2017, 06:00

The problem of (not drinking enough) Dom (Perignon) :thumbup:
xuezhe
Posts: 91
Joined: 06 Jan 2016, 11:02

Re: The problem of Dom

14 Nov 2017, 07:46

teadrinker wrote:The problem is that you try to get domElementObject before the document is loaded.
I know.But how to change it to funtion.
teadrinker
Posts: 4391
Joined: 29 Mar 2015, 09:41
Contact:

Re: The problem of Dom

14 Nov 2017, 08:01

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
OnExit( Func("Exit").Bind(wb) )
wb.Visible := 1   
wb.Navigate("https://www.baidu.com")  

elem := DomElementReady(10000)
MsgBox % IsObject(elem)

DomElementReady(Milliseconds := 5000){
   global wb
   StartTime := A_TickCount
   while wb.busy
   {
      Sleep 50
      ElapsedTime := A_TickCount - StartTime
      if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
   }
    Loop {
         Sleep 50
          ElapsedTime := A_TickCount - StartTime
          if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
          element :=wb.document.getElementById("kw")
   } Until IsObject(element)
   Return element
}
 
Exit(ie)  {
   try ie.Quit()
}
xuezhe
Posts: 91
Joined: 06 Jan 2016, 11:02

Re: The problem of Dom

14 Nov 2017, 08:09

teadrinker wrote:

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
OnExit( Func("Exit").Bind(wb) )
wb.Visible := 1   
wb.Navigate("https://www.baidu.com")  

elem := DomElementReady(10000)
MsgBox % IsObject(elem)

DomElementReady(Milliseconds := 5000){
   global wb
   StartTime := A_TickCount
   while wb.busy
   {
      Sleep 50
      ElapsedTime := A_TickCount - StartTime
      if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
   }
    Loop {
         Sleep 50
          ElapsedTime := A_TickCount - StartTime
          if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
          element :=wb.document.getElementById("kw")
   } Until IsObject(element)
   Return element
}
 
Exit(ie)  {
   try ie.Quit()
}
thx.
the object of dom may geted by "getELementById" also "getElementsByClassName".
for example
the first page I'll use getElementById
the sencond page I'll use getElementsByClassName
the third page I'll use getElmentsByTagName
I want to use a function not three functions.

Code: Select all

element :=domElementObject
Last edited by xuezhe on 14 Nov 2017, 08:15, edited 1 time in total.
teadrinker
Posts: 4391
Joined: 29 Mar 2015, 09:41
Contact:

Re: The problem of Dom

14 Nov 2017, 08:15

Can you show your code, where you get an element through getElementsByClassName and getElmentsByTagName?
xuezhe
Posts: 91
Joined: 06 Jan 2016, 11:02

Re: The problem of Dom

14 Nov 2017, 08:22

teadrinker wrote:Can you show your code, where you get an element through getElementsByClassName and getElmentsByTagName?
sure.

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := 1   
wb.Navigate("https://www.baidu.com")  
sleep 3000
Msgbox wb.document.getElementsByTagName("input")[8].value
there're three sites,I want to use the same function.
the first is used by "getElmentById"
the sencond is used by "getElmentsByName"
the third is used by "getElmentsByClassName"
teadrinker
Posts: 4391
Joined: 29 Mar 2015, 09:41
Contact:

Re: The problem of Dom

14 Nov 2017, 09:23

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
OnExit( Func("Exit").Bind(wb) )
wb.Visible := 1   
wb.Navigate("https://www.baidu.com")  

elem := DomElementReady("getElementById", "kw",, 10000)
MsgBox % IsObject(elem)

DomElementReady(method, value, index := 0, Milliseconds := 5000){
   global wb
   StartTime := A_TickCount
   while wb.busy
   {
      Sleep 50
      ElapsedTime := A_TickCount - StartTime
      if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
   }
    Loop {
         Sleep 50
          ElapsedTime := A_TickCount - StartTime
          if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
          obj := (wb.document)[method](value)
          element := (method = "getElementById") ? obj : obj[index]
   } Until IsObject(element)
   Return element
}
 
Exit(ie)  {
   try ie.Quit()
}
or

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
OnExit( Func("Exit").Bind(wb) )
wb.Visible := 1   
wb.Navigate("https://www.baidu.com")  

elem := DomElementReady("getElementsByTagName", "input", 8, 10000)
if IsObject(elem)
   MsgBox, % elem.value
else
   MsgBox, Failed to get element

DomElementReady(method, value, index := 0, Milliseconds := 5000){
   global wb
   StartTime := A_TickCount
   while wb.busy
   {
      Sleep 50
      ElapsedTime := A_TickCount - StartTime
      if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
   }
    Loop {
         Sleep 50
          ElapsedTime := A_TickCount - StartTime
          if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
          obj := (wb.document)[method](value)
          element := (method = "getElementById") ? obj : obj[index]
   } Until IsObject(element)
   Return element
}
 
Exit(ie)  {
   try ie.Quit()
}
xuezhe
Posts: 91
Joined: 06 Jan 2016, 11:02

Re: The problem of Dom

14 Nov 2017, 23:48

teadrinker wrote:

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
OnExit( Func("Exit").Bind(wb) )
wb.Visible := 1   
wb.Navigate("https://www.baidu.com")  

elem := DomElementReady("getElementById", "kw",, 10000)
MsgBox % IsObject(elem)

DomElementReady(method, value, index := 0, Milliseconds := 5000){
   global wb
   StartTime := A_TickCount
   while wb.busy
   {
      Sleep 50
      ElapsedTime := A_TickCount - StartTime
      if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
   }
    Loop {
         Sleep 50
          ElapsedTime := A_TickCount - StartTime
          if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
          obj := (wb.document)[method](value)
          element := (method = "getElementById") ? obj : obj[index]
   } Until IsObject(element)
   Return element
}
 
Exit(ie)  {
   try ie.Quit()
}
or

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
OnExit( Func("Exit").Bind(wb) )
wb.Visible := 1   
wb.Navigate("https://www.baidu.com")  

elem := DomElementReady("getElementsByTagName", "input", 8, 10000)
if IsObject(elem)
   MsgBox, % elem.value
else
   MsgBox, Failed to get element

DomElementReady(method, value, index := 0, Milliseconds := 5000){
   global wb
   StartTime := A_TickCount
   while wb.busy
   {
      Sleep 50
      ElapsedTime := A_TickCount - StartTime
      if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
   }
    Loop {
         Sleep 50
          ElapsedTime := A_TickCount - StartTime
          if(ElapsedTime>Milliseconds)
               Return -1  ;超时,返回为-1
          obj := (wb.document)[method](value)
          element := (method = "getElementById") ? obj : obj[index]
   } Until IsObject(element)
   Return element
}
 
Exit(ie)  {
   try ie.Quit()
}
thx. :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 181 guests