AutoHotkey Community

It is currently May 25th, 2012, 10:52 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: July 6th, 2007, 5:40 pm 
Offline

Joined: July 6th, 2007, 5:24 pm
Posts: 23
I've written some scripts that are capable of determining if the page is done loading using this script I found on the forums:

Code:
WinWait, Webpage - Microsoft Internet Explorer
IfWinNotActive
      WinActivate
WinWaitActive
Loop {
Sleep 200
Amt++
If (Amt = 5 AND Check = "Done|Done|Done|Done|")
   {
 Amt = 0
Break
}
Else If (Amt = 5 AND Check <> "Done|Done|Done|Done|")
 { Check =
  Amt = 1
 }
StatusBarGetText, IEpageStatus, 1, Webpage - Microsoft Internet Explorer
If IEpageStatus = Done
Check = %Check%Done|
Else
Check = %Check%/\
}


This has worked fine in the past, but now I run into a snag with the new script I'm writing because IE normally displays "Done" when it is finished loading, but occasionally there is an error and IE will display "REFRESH ERROR" even though the page has completely loaded fine. My coding knowledge is VERY limited, so I tried modifying it like this, but it didn't work:

Code:
WinWait, Webpage - Microsoft Internet Explorer
IfWinNotActive
      WinActivate
WinWaitActive
Loop {
Sleep 200
Amt++
If (Amt = 5 AND Check = "Done|Done|Done|Done|" OR "REFRESH ERROR|REFRESH ERROR|REFRESH ERROR|REFRESH ERROR|")
   {
 Amt = 0
Break
}
Else If (Amt = 5 AND Check <> "Done|Done|Done|Done|" OR "REFRESH ERROR|REFRESH ERROR|REFRESH ERROR|REFRESH ERROR|")
 { Check =
  Amt = 1
 }
StatusBarGetText, IEpageStatus, 1, Webpage - Microsoft Internet Explorer
If IEpageStatus = Done
Check = %Check%Done|
If IEpageStatus = REFRESH ERROR
Check = %Check%REFRESH ERROR|
Else
Check = %Check%/\
}


How can I make the sript know the page is completely loaded when it says "Done" OR if it says "REFRESH ERROR"?? Any help would be much appreciated, thanks!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 6th, 2007, 5:55 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
untested
Code:
WinWait, Webpage - Microsoft Internet Explorer
IfWinNotActive
      WinActivate
WinWaitActive
Loop {
Sleep 200
Amt++
If (Amt = 5 AND (Check = "Done|Done|Done|Done|" OR Check = "REFRESH ERROR|REFRESH ERROR|REFRESH ERROR|REFRESH ERROR|"))
   {
 Amt = 0
Break
}
Else If (Amt = 5 AND (Check <> "Done|Done|Done|Done|" AND  Check <> "REFRESH ERROR|REFRESH ERROR|REFRESH ERROR|REFRESH ERROR|"))
 { Check =
  Amt = 1
 }
StatusBarGetText, IEpageStatus, 1, Webpage - Microsoft Internet Explorer
If IEpageStatus = Done
Check = %Check%Done|
Else If IEpageStatus = REFRESH ERROR
Check = %Check%REFRESH ERROR|
Else
Check = %Check%/\
}

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 6th, 2007, 6:00 pm 
Offline

Joined: December 30th, 2005, 5:01 pm
Posts: 448
Change your code according to this:
Code:
If (Amt = 5 AND (Check = "Done|Done|Done|Done|" OR Check = "REFRESH ERROR|REFRESH ERROR|REFRESH ERROR|REFRESH ERROR|"))

Yeah, just follow what engunneer said. (he posted just a bit quicker then me:).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 6th, 2007, 6:18 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
I made a few other changes too. At the end, if the Check read "Done", the check for Refresh Error would append \

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 6th, 2007, 8:54 pm 
Offline

Joined: March 16th, 2005, 10:33 pm
Posts: 968
Location: Frisia
Determine if a WebPage is completely loaded in IE

_________________
Image mirror 1mirror 2mirror 3ahk4.me • PM or Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2007, 2:09 pm 
Offline

Joined: July 6th, 2007, 5:24 pm
Posts: 23
daonlyfreez,

Can you explain to me how that script works? I have very limited scripting knowledge, and that script may as well be greek to me....

How do I set it up so it knows what window to check to see if it is done reloading? I've done some investigating and I understand there's a program called winspector that may come into play here. I downloaded winspector, but I don't really know how to use it to work with that script :(


Any help much appreciated!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2007, 5:46 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
did my code not work?

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2007, 7:02 pm 
Offline

Joined: July 6th, 2007, 5:24 pm
Posts: 23
Well, it worked the first time the page would display "REFRESH ERROR," yes. However, I didn't realize that once that error is displayed, if you click on a separate link on the page, IE will open that link, but it will STILL display "REFRESH ERROR" in the status bar even WHILE the page reloads. So the script sees "REFRESH ERROR" is already there and thinks the page is done reloading when it is not.

From what I understand, the other script that daonlyfreez posted is much better and more reliable, and I would like to implement it in my script, but I have no idea how to do that, so I'm stuck right now. I posted a thread here about it: http://www.autohotkey.com/forum/viewtopic.php?t=20964

My main problem is the script itself is like greek to me. I tried just simply copy/pasting it in place of my current wait-for-refresh code with no success.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2007, 8:01 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
your best bet is to past your code here in the forum, and we can see how you pasted in that function. I don't understand that function myself, but it's a lot easier to troubleshoot when we can see it.

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2007, 9:00 pm 
Offline

Joined: July 6th, 2007, 5:24 pm
Posts: 23
Well, part of my job involves filling out the same online form with the same general information over and over again. So instead of clicking somwhere, pasing this or that information, clicking there, pasting something else, I have a script set up so that when I hit a key, it goes down and fills in the appropriate information in the appropriate field. I accomplish this by simply telling it to tab a certain amount of times, then paste a variable that I define at the begging of the script.

Part of the online system requires the script to wait for the page to reload before pasting the rest of the information.

So my script is something like this:

First I define certain variables at the start.
x = 13
y = 155t64-9

1) Paste x variable
2) Wait for the internet page to reload
3) Press Tab 5 times to get to the correct field
4) Paste y variable
5) Wait for page to reload
6) Select a certain item from a drop-down menu

etc. etc.

Here's a portion of my script so that you know kinda what it does. Please excuse the rudimentary nature of it :)

Code:
NumpadRight::
{
Send {Tab}%Engrg%
Sleep, 200
Send {Tab}{Tab}{Tab}N/A
Sleep, 200
Send {Tab}{Tab}N/A
Sleep, 200
Send {Tab}{Tab}{Enter}
;;;;;;;;;;;;;;;;;;;;
;;;;;;START PAGE RELOAD
;;;;;;;;;;;;;;;;;;;;
Winwait Webpage - Microsoft Internet Explorer
Loop {
Sleep 200
Amt++
If (Amt = 5 AND Check = "Done|Done|Done|Done|")
   {
 Amt = 0
Break
}
Else If (Amt = 5 AND Check <> "Done|Done|Done|Done|")
 { Check =
  Amt = 1
 }
StatusBarGetText, IEpageStatus, 1, Webpage - Microsoft Internet Explorer
If IEpageStatus = Done
Check = %Check%Done|
Else
Check = %Check%/\
}
;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;END RELOAD
;;;;;;;;;;;;;;;;;;;;;;;
Send {Tab}{Tab}{Tab}{Tab}{Tab}{Tab}{Enter}
}
Return


So the above code navigates a webpage using the tab key and pastes certain information where required.

The portion of script that I pasted at the begging of this thread is what I used to wait for the page to reload (you can see it in the above code as well). Unfortunately, that portion of code does not work perfectly 100% of the time. Sometimes the online system is running slowly, and the script "thinks" the page is reloaded when it really isn't. This causes the script to run the next portion of the code prematurely, which messes up the entire process.

Also, recently the online system has been giving me the "REFRESH ERROR" message, and when that happens, my script doesn't work at all because there is no way for it to know that the page is finished reloading based on the status bar message.

So, I looked around the forums for something much better and a little less "dirty" than the 'wait-for-refresh' portion of code that I currently have. I found the thread that daonlyfreez posted, and I thought would be very helpful if I could implement it into my current script:

http://www.autohotkey.com/forum/viewtopic.php?t=19256

Unfortunately, it is above my head in terms of code knowledge, so I don't know how to implement it in my current script. I tried just copying and pasting that snippet of code in place of what I currently have for the 'wait-for-reload' with no success. I need a bit of help so I can figure out how to implement the code that daonlyfreez posted into my current script instead of having the dirty/unreliable code I currently have.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2007, 9:20 pm 
Offline

Joined: March 16th, 2005, 10:33 pm
Posts: 968
Location: Frisia
You can use a combination of IE and JavaScript to do form-filling. You can use it with other browsers too, but IE can be controlled (somewhat sofar) through COM, you can even create your own IE window in an AHK Gui.

Since form-filling is a much wanted, I might be able to come up with a more clearer example of what to look for.

In the meantime you could search for JavaScript/Bookmarklets to fill out the forms, since we cannot do DOM manipulation yet, and check out Sean's IE control.

For both (determining whether IE is loaded, or adding IE to your AHK Gui), you'll need Sean's CoHelper, as an #include.

Check his examples, they are quite self-explanatory.

_________________
Image mirror 1mirror 2mirror 3ahk4.me • PM or Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2007, 9:23 pm 
Offline

Joined: July 6th, 2007, 5:24 pm
Posts: 23
daonlyfreez,

Like I said, I know very little about coding, so those programs posted to me made absolutely no sense to me, and I had no idea how to use them to help my page-refresh problem. :(

Is the case juse that the code is too difficult and I'm in way over my head?

I'm not really interested in a form-filling program, I just want to somehow come across a snippet of code that knows when an IE page is refreshed that works 100% of the time (as opposed to about 80% of the time with the current snippet I have now).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2007, 9:27 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
you have stated that the REFRESH ERROR message stays up, so there is NO way of using that to tell if the page is loaded. If it says Refresh Error, maybe you can refresh or reopen the page until it reads Done again?

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2007, 10:37 pm 
Offline

Joined: June 14th, 2007, 6:43 pm
Posts: 11
Woot I finally get to help someone ... I had this same problem for the longest time.

To get the "Determine if a WebPage is completely loaded in IE" to work first save this as CoHelper.ahk
Code:
/*
CoHelper.ahk
*/

VTable(ppv, idx)
{
   ptr := (*ppv | *++ppv << 8 | *++ppv << 16 | *++ppv << 24) + 4*idx
   Return  *ptr | *++ptr << 8 | *++ptr << 16 | *++ptr << 24
}

QueryInterface(ppv, ByRef IID)
{
   If   StrLen(IID) = 38
   GUID4String(IID, IID)
   DllCall(VTable(ppv,0), "Uint", ppv, "str", IID, "UintP", ppv)
   Return ppv
}

AddRef(ppv)
{
   Return DllCall(VTable(ppv,1), "Uint", ppv)
}

Release(ppv)
{
   Return DllCall(VTable(ppv,2), "Uint", ppv)
}

QueryService(ppv, ByRef SID, ByRef IID)
{
   If   StrLen(SID) = 38
   GUID4String(SID, SID)
   If   StrLen(IID) = 38
   GUID4String(IID, IID)
   GUID4String(IID_IServiceProvider, "{6D5140C1-7436-11CE-8034-00AA006009FA}")
   DllCall(VTable(ppv,0), "Uint", ppv, "str", IID_IServiceProvider, "UintP", psp)
   DllCall(VTable(psp,3), "Uint", psp, "str", SID, "str", IID, "UintP", ppv)
   DllCall(VTable(psp,2), "Uint", psp)
   Return ppv
}

GetIDispatch(pdisp, LCID = 0)
{
   DllCall(VTable(pdisp, 4), "Uint", pdisp, "Uint", 0, "Uint", LCID, "UintP", pinfo)
   If !pinfo
   Return
   DllCall(VTable(pinfo, 3), "Uint", pinfo, "UintP", pattr)
   IDispatch := String4GUID(pattr)
   DllCall(VTable(pinfo,19), "Uint", pinfo, "Uint" , pattr)
   DllCall(VTable(pinfo, 2), "Uint", pinfo)
   Return IDispatch
}

Ansi2Unicode(ByRef sString, ByRef wString, nLen = 0)
{
   If !nLen
       nLen := DllCall("MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &sString, "int", -1, "Uint", 0, "int", 0)
   VarSetCapacity(wString, nLen * 2 + 1)
   DllCall("MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &sString, "int", -1, "Uint", &wString, "int", nLen)
}

Unicode2Ansi(ByRef wString, ByRef sString, nLen = 0)
{
   pString := wString + 0 > 65535 ? wString : &wString
   If !nLen
       nLen := DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "Uint", 0, "int", 0, "Uint", 0, "Uint", 0)
   VarSetCapacity(sString, nLen)
   DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "str", sString, "int", nLen, "Uint", 0, "Uint", 0)
}

CLSID4ProgID(Byref CLSID, sProgID)
{
   VarSetCapacity(CLSID, 16)
   Ansi2Unicode(sProgID, wProgID)
   DllCall("ole32\CLSIDFromProgID", "str", wProgID, "str", CLSID)
}

GUID4String(Byref CLSID, sString)
{
   VarSetCapacity(CLSID, 16)
   Ansi2Unicode(sString, wString, 38)
   DllCall("ole32\CLSIDFromString", "str", wString, "str", CLSID)
}

String4GUID(Byref CLSID)
{
   VarSetCapacity(wString, 38 * 2 + 1)
   DllCall("ole32\StringFromGUID2", "str", CLSID, "str", wString, "int", 38 + 1)
   Unicode2Ansi(wString, sString, 38)
   Return sString
}

CreateObject(ByRef CLSID, ByRef IID, CLSCTX = 5)
{
   If   StrLen(CLSID)  =  38
   GUID4String(CLSID, CLSID)
   If   StrLen(IID) = 38
   GUID4String(IID, IID)
   DllCall("ole32\CoCreateInstance", "str", CLSID, "Uint", 0, "Uint", CLSCTX, "str", IID, "UintP", ppv)
   Return ppv
}

ActiveXObject(ProgID, CLSCTX = 5)
{
   InStr(ProgID, "{") ? GUID4String(CLSID, ProgID) : CLSID4ProgID(CLSID, ProgID)
   GUID4String(IID_IDispatch, "{00020400-0000-0000-C000-000000000046}")
   DllCall("ole32\CoCreateInstance", "str", CLSID, "Uint", 0, "Uint", CLSCTX, "str", IID_IDispatch, "UintP", pdisp)
   Return pdisp
}

GetObject(Moniker)
{
   Ansi2Unicode(Moniker, wMoniker)
   GUID4String(IID_IDispatch, "{00020400-0000-0000-C000-000000000046}")
   DllCall("ole32\CoGetObject", "str", wMoniker, "Uint", 0, "str", IID_IDispatch, "UintP", pdisp)
   Return pdisp
}

GetActiveObject(ProgID)
{
   InStr(ProgID, "{") ? GUID4String(CLSID, ProgID) : CLSID4ProgID(CLSID, ProgID)
   GUID4String(IID_IDispatch, "{00020400-0000-0000-C000-000000000046}")
   DllCall("oleaut32\GetActiveObject", "str", CLSID, "Uint", 0, "UintP", punk)
   DllCall(VTable(punk,0), "Uint", punk, "str", IID_IDispatch, "UintP", pdisp)
   DllCall(VTable(punk,2), "Uint", punk)
   Return pdisp
}

SysAllocString(sString)
{
   Ansi2Unicode(sString, wString)
   Return DllCall("oleaut32\SysAllocString", "str", wString)
}

SysFreeString(pString)
{
   Return DllCall("oleaut32\SysFreeString", "Uint", pString)
}

CoTaskMemAlloc(cb)
{
   Return DllCall("ole32\CoTaskMemAlloc", "Uint", cb)
}

CoTaskMemFree(pv)
{
   Return DllCall("ole32\CoTaskMemFree", "Uint", pv)
}

CoCreateGuid()
{
   VarSetCapacity(GUID, 16)
   DllCall("ole32\CoCreateGuid", "Uint", &GUID)
   Return String4GUID(GUID)
}

CoInitialize()
{
   DllCall("ole32\CoInitialize", "Uint", 0)
}

CoUninitialize()
{
   DllCall("ole32\CoUninitialize")
}

OleInitialize()
{
   DllCall("ole32\OleInitialize", "Uint", 0)
}

OleUninitialize()
{
   DllCall("ole32\OleUninitialize")
}

DecodeInteger(ref, nSize = 4)
{
   DllCall("RtlMoveMemory", "int64P", val, "Uint", ref, "Uint", nSize)
   Return val
}

EncodeInteger(ref, val, nSize = 4)
{
   DllCall("RtlMoveMemory", "Uint", ref, "int64P", val, "Uint", nSize)
}

DecodeReal(ref, nSize = 4)
{
   DllCall("RtlMoveMemory", nSize = 4 ? "floatP" : "doubleP", val, "Uint", ref, "Uint", nSize)
   Return val
}

EncodeReal(ref, val, nSize = 4)
{
   DllCall("RtlMoveMemory", "Uint", ref, nSize = 4 ? "floatP" : "doubleP", val, "Uint", nSize)
}


Then save this as WebsiteFinished.ahk (or another name)
Code:
#Include CoHelper.ahk

CoInitialize()

;ControlGet, hIESvr, hWnd, , Internet Explorer_Server1, ahk_class IEFrame
MouseGetPos, , , , hIESvr, 2

If !hIESvr
   ExitApp

GUID4String(IID_IHTMLDocument2, "{332C4425-26CB-11D0-B483-00C04FD90119}")
GUID4String(IID_IWebBrowser2  , "{D30C1661-CDAF-11D0-8A3E-00C04FC9E26E}")
GUID4String(SID_SWebBrowserApp, "{0002DF05-0000-0000-C000-000000000046}")

DllCall("SendMessageTimeout", "Uint", hIESvr
   , "Uint", DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
   , "Uint", 0, "Uint", 0, "Uint", 2, "Uint", 1000, "UintP", lResult)
DllCall("oleacc\ObjectFromLresult", "Uint", lResult, "str", IID_IHTMLDocument2, "int", 0, "UintP", phd)
pwb := QueryService(phd, SID_SWebBrowserApp, IID_IWebBrowser2)
Release(phd)
If !phd || !pwb
   ExitApp

/*
   READYSTATE_UNINITIALIZED = 0      ; Default initialization state.
   READYSTATE_LOADING       = 1      ; Object is currently loading its properties.
   READYSTATE_LOADED        = 2      ; Object has been initialized.
   READYSTATE_INTERACTIVE   = 3      ; Object is interactive, but not all of its data is available.
   READYSTATE_COMPLETE      = 4      ; Object has received all of its data.
*/
Loop
{
   DllCall(VTable(pwb, 56), "Uint", pwb, "intP", nReady)
   If nReady = 4
      Break
   Sleep, 500
   if (A_Index > 20)
   {
      MsgBox, %nReady%
      break
   }
}

Release(pwb)
CoUninitialize()

; MsgBox, Ready



Then in your program while the page is loading use a RunWait command to the WebpageFinished.ahk program i.e.
Code:
RunWait, %path%WebsiteFinished.ahk


The program will wait until the page has finished.

Another way I have found to pause the program while the page is loading is to use ImageSearch. Just take a screenshot of a part of the webpage that is static and do something like this.
Code:
    Loop
    {
        ImageSearch, FoundX, FoundY, 0,0, A_ScreenWidth, A_ScreenHeight, %path%Images\BackCopyNextButton.bmp
        if ErrorLevel = 2
        {
            ErrorMsg := "Could not find BackCopyNextButton.bmp"
            Goto, ErrorRtn
        }
        else if ErrorLevel = 1
        {
            if (NotFound > 20)
            {
                ErrorMsg := "Could not find BackCopyNextButton.bmp on screen"
                Goto, ErrorRtn
            }
            NotFound += 1           
            Sleep, 100
        }
        else
        {
            Break
        }
    }


Hope this helps :D
Bennoman


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 11th, 2007, 5:20 pm 
Offline

Joined: July 6th, 2007, 5:24 pm
Posts: 23
bennoman,

Thanks for you help so far, but I still can't get that to work!

To test the script, I made CoHelper.ahk, WebsiteFinished.ahk (copied just like you posted it), and test.ahk and put them all in C:/tags/

In test.ahk, I put this:

Code:
#a::
{
Send F5
RunWait %path%WebsiteFinished.ahk
Msgbox ready
}


And when I use the hotkey, I get an error saying it was unable to launch the program, so I tried this:

Code:
#a::
{
Send F5
RunWait C:/tags/WebsiteFinished.ahk
msgbox ready
}
return


The new program I wrote doesn't refresh the page for some reason, and it just instantly displays the "ready" message box.

What am I doing wrong?


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], mrhobbeys, toddintr and 75 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group