The one issue I've run into is this: Say there's an input box called
Spam, that requires a value. If you click the Go button and Spam is
empty, a little javascript alert box pops up and says "Hey, fill in
Spam!"
well, if I load the page using python and click the Go button with
Spam empty, the javascript alert pops up... and I don't know how to
get rid of it. Python hangs until I manually clear the alert.
Any ways around this?
Thanks
--B
Well, the way I get around this feels like a horrible, horrible hack, but
it works pretty well. Below is some code from a class I made called
WebSerf.
Use the code in CaptureWindowEvents to install new window.alert and
window.open functions (I call this method after I click a Submit button,
click a link, or navigate to a whole new page - be sure the new document
has fully loaded first!).
The code in GetLatestAlertMessage and GetLatestOpenString let you see what
actions the page tried to take.
You'll have to modify the code somewhat to work with your approach,
but I hope it at least helps in some way.
-Dave
The code:
CAPTURE_JSCRIPT = '''var serfcapturedevents = 1;
var serfalertmsg="";
var serfopenwin=null;
function serfalert(arg) { serfalertmsg=arg; }
var serfcapturedevents = 1;
var serfalertmsg="";
var serfopenwin=null;
function serfalert(arg) { serfalertmsg=arg; }
function serfopen()
{
var args = new Array(null, null, null, null);
fargs = serfopen.arguments;
for (i=0; i < fargs.length; i++)
args[i] = fargs[i];
serfopenwin = serfoldwindowopen(args[0], args[1], args[2], args[3]);
return serfopenwin;
}
function serfclosepopup()
{ if (serfopenwin != null)
serfopenwin.close();
}
window.attachEvent('onunload', serfclosepopup);
window.alert=serfalert;
serfoldwindowopen = window.open;
window.open=serfopen;
var serfpromptvalue = '';
function serfprompt() { return serfpromptvalue; }
window.prompt = serfprompt;
var serfconfirmvalue = true;
function serfconfirm() { return serfconfirmvalue; }
window.confirm = serfconfirm;'''
Now a few methods from the WebSerf class:
def CaptureWindowEvents(self):
'Installs a Javascript routine to reroute any window.alert/.open
calls'
pw = self.doc.parentWindow
try:
pw.serfcapturedevents
return # Already captured
except AttributeError:
pass # Ok, now go capture them
try:
pw.execScript(CAPTURE_JSCRIPT)
except:
traceback.print_exc()
def GetLatestAlertMsg(self):
'Returns and clears the most recent window.alert message'
try:
pw = self.doc.parentWindow
msg = pw.serfalertmsg
if msg:
pw.serfalertmsg = ''
except:
msg = ''
traceback.print_exc()
return msg
def GetLatestOpenString(self):
'Returns and clears the most recent window.open string or None'
try:
pw = self.doc.parentWindow
win = pw.serfopenwin
if win:
pw.serfopenwin = None
except:
win = None
traceback.print_exc()
return win
> Well, the way I get around this feels like a horrible, horrible hack, but
> it works pretty well. Below is some code from a class I made called
> WebSerf.
[snip]
Well, maybe it is a hack. But since it worked beautifully, I'm
certainly not going to judge 8)
Thanks
--B