""" PyKHTML does not allow alerts and prompts to interfere with the page running -- even more than that, it allows you to handle calls to alert yourself! """ # in case pykhtml isn't already installed import sys sys.path.append("..") import pykhtml page = """ Alert Example """ def alertHandler(browser, text): #import time #browser.eval("window.foobaz = 'hi'") print "alert:", text # if the text of the alert was 'Another alert', then # we create one morealert to showcase the fact that we # can evaluate javascript dynamically if text == 'Another alert': browser.eval("alert('Dynamic alert!')") # else if the text was 'Dynamic alert!' (from the alert # we ran dynamically ;)), then that's the last alert. # Stop PyKHTML elif text == 'Dynamic alert!': #print "stopping" pykhtml.stopEventLoop() def promptHandler(text, defaultText): print "prompt: %s (default %s)" % (text, repr(defaultText)) if text == "What is your name?": return "Paul" def main(): browser = pykhtml.Browser() # install our function be called when there are # alerts/prompts in the JavaScript source code browser.onAlert = pykhtml.partial(alertHandler, browser) browser.onPrompt = promptHandler # load our markup browser.setHtml(page) #pykhtml.timer(3, pykhtml.stopEventLoop) pykhtml.startEventLoop() if __name__ == "__main__": main()