Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

automation - How do I wait for Google Chrome to load a webpage before continuing in AutoHotkey?

I am working on a AutoHotkey script that does a repetitive job in Google Chrome. Every time that I click a link from the script, I have to tell my script to sleep while the page loads.

I am wondering if there is a way in AHK for me to tell the script to wait for the browser to finish loading the page rather than sleeping for a set amount of time. Is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While Karthik's answer is better than a sleep command, and it can function fairly well for many sites, I found that there are some problems which can creep up especially if you use this quite often.

I needed a reliable solution myself, which I believe I've finally found. Go to the Chrome Web Store and add the Google Chrome extension called Control Freak. This easily allows you to set up code chunks which can be triggered on a single page, a whole domain, or any site/page.

Control Freak Chrome Extension

Once you've got it loaded, click the cog wheel button you now have for the extension. Click at the top of the pop-up window in the appropriate context/space you need to have the page load detection. i.e. All

Now you can click on the Libs tab. Scroll down to jQuery. You can pick any version from the list you like, but I chose the latest. Notice that the url for the CDN is added at the bottom. Now after you click the Save button at the bottom you will always have jQuery available in whatever context was chosen.

Switch back to the JavaScript tab and add this code, and modify to how you like:

jQuery(function() {
  prompt("Diagnostic: Page Status", "loaded");
});

Once you click Save, this code will execute upon the jQuery Ready event. Now you have a reliable way to detect the page load, which AHK can use!

With this in place, you can have AutoHotkey wait for a window who's title starts by saying "The page at"...., which will undoubtedly be our prompt, pre-filled with our word "loaded". You can tell AHK to send a Control+c to copy that, then check its value, or just presume that since you saw it, then you likely have what you expected. The following AHK code can wait for the prompt pop-up (which could just as easily be done with an alert btw).

clipboard=
WinWait, The page at
WinWaitActive
Send ^c
ClipWait
WinClose
; The rest here is entirely optional tooltip code, but can be useful.
tooltip, The diagnostic data provided was: %clipboard%,0,0
sleep, 1000
tooltip

So let's optimize it and put it into a function you can call over and over again:

Function Definition:

waitForPageLoad()
{
  WinWait, The page at
  WinWaitActive
  WinClose
}

Function Call:

waitForPageLoad()

Other thoughts..

Now I've noticed this code to trigger even at times when the page isn't truly being changed or reloaded, such as when the page URL might change, but they specifically coded it to not leave the page, and typically you see new content there. That is perfect for my purposes, but this could be filtered out by setting some kind of variable and checking if it had already been set (within the JavaScript code you added in Control Freak).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...