views:

42

answers:

2

Hi , Is there any way that I can find out using webbrowser or anyother package in Python if a browser window that I've opened has loaded completely or not? Once it's loaded completely I want to take a screenshot of it and save it.

A: 

Strictly speaking you can't do that with python. However, using Javascript you can wait for the "onload" event and send an ajax request to your python backend. With JQuery it should be something like.

//callback for the onload
jQuery(document)
        .ready(function() {

$.ajax({
   type: "POST",
   url: "thePythonScript.py",
   data: "name=Daniel&location=Somewhere",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

});
Dani Cricco
I do not want to use anything else than Python packages. Is there anyway around this problem with win32api?thanks for the help.
fixxxer
If the browser is running in a different machine, then JS is your only option. Why you don't want to use JS for this ?
Dani Cricco
primarily because I havn't used JS ever and I want to learn more about Python's bells and whistles than that of JS right now.
fixxxer
understood, you have a more academic motivation. Good luck!
Dani Cricco
+2  A: 

You can do this using e.g. Selenium; I'm not sure if it's what you want, though. See this short guide.

#!/usr/bin/env python

from selenium import selenium

sel = selenium('localhost', 4444, '*firefox', 'http://www.google.com/')
sel.start()
sel.open('/')
sel.wait_for_page_to_load(10000)
sel.stop()

You could also use COM hooks to control IE (ugh):

import win32com.client
import time

ie = win32com.client.Dispatch( "InternetExplorer.Application" )
ie.Navigate( <some URL> )
time.sleep( 1 )
print( ie.Busy )

There's a module that wraps all the COM functionality: IEC.py.

katrielalex
What does "sel.wait_for_page_to_load(10000)" actually mean? Would it mean I have to wait out 10000 seconds or is it like the maximum time limit?
fixxxer
I liked your second suggestion, because then I wouldn't need to install anything new.The only problem being - except for the first time I'd get this error - "pywintypes.com_error: (-2147023174, 'The RPC server is unavailable.', None, None)"
fixxxer
@fixxxer: it's a timeout. The selenium docs will tell you more. I would try Googling the RPC server error, I think you need to start some services. Can you access the page in IE normally?
katrielalex
@katrielalex - yes, I'm able to open the page in IE normally.I wouldn't have commented the RPC problem without Googling it. :) thx
fixxxer