Executing Javascript function in a web page in IE

The following code executes a Javascript function on a web page using IE and COM.

import win32com.client, pythoncom
from time import sleep

ie=win32com.client.Dispatch('internetexplorer.application')
ie.Visible=1
ie.Navigate('http://www.cpplab.com/Articles/JSCalls/TestPage/JSCallTestPage.htm')
sleep(5) # Give it time to get there.

id=ie.Document.Script._oleobj_.GetIDsOfNames('Multiply') # 'Multiply' is the function name.
#5 and 2 below are the parameters passed to the function 'Multiply'.
res=ie.Document.Script._oleobj_.Invoke(id, 0, pythoncom.DISPATCH_METHOD, True, 5, 2)

Leave a Reply

Your email address will not be published. Required fields are marked *