pythoncom and IDispatch

F

fraca7

Hello. I got a little problem while using pythoncom to automate IE; for
some reason, changing the 'selectedIndex' on an instance of
IHTMLSelectElement doesn't fire the 'onchange' event (I guess this is a
bug in mshtml).

So, I tried to get the 'onchange' event handler and call it myself.
According to the docs, this is a simple IDispatch implementation and
calling Invoke() should do the trick; I actually have a working example
of this in Delphi.

But I can't manage to get it work in Python; the following code


idisp = pythoncom.WrapObject(elt.onchange)
idisp.Invoke(pythoncom.DISPID_VALUE,
0x400, # LOCALE_USER_DEFAULT
pythoncom.DISPATCH_METHOD,
False)

fails with an AttributeError:

Traceback (most recent call last):

File "C:\Python22\Lib\site-packages\win32com\server\policy.py", line
283, in _
Invoke_

return self._invoke_(dispid, lcid, wFlags, args)

File "C:\Python22\Lib\site-packages\win32com\server\policy.py", line
288, in _
invoke_

return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None,
None)
File "C:\Python22\Lib\site-packages\win32com\server\policy.py", line
581, in _
invokeex_

return func(*args)

File "ietest.py", line 44, in OnDocumentComplete

self.deleg.onDocumentComplete(Dispatch(disp), url)

File "ietest.py", line 122, in onDocumentComplete

self.current.onDocumentComplete(self, browser, url)

File "ietest.py", line 141, in onDocumentComplete

sink.nextStep()

File "ietest.py", line 96, in nextStep

self.current.onStart(self)

File "ietest.py", line 191, in onStart

False)

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Python
COM Serve
r Internal Error', 'Unexpected Python Error: exceptions.AttributeError:
_Invoke_
', None, 0, -2147467259), None)


Did I miss something ?

TIA
 
R

Roger Upole

fraca7 said:
Hello. I got a little problem while using pythoncom to automate IE; for some reason, changing the 'selectedIndex' on an
instance of IHTMLSelectElement doesn't fire the 'onchange' event (I guess this is a bug in mshtml).

As I understand it, this was done as a security measure to foil script
exploits.
So, I tried to get the 'onchange' event handler and call it myself. According to the docs, this is a simple IDispatch
implementation and calling Invoke() should do the trick; I actually have a working example of this in Delphi.

But I can't manage to get it work in Python; the following code


idisp = pythoncom.WrapObject(elt.onchange)
idisp.Invoke(pythoncom.DISPID_VALUE,
0x400, # LOCALE_USER_DEFAULT
pythoncom.DISPATCH_METHOD,
False)

fails with an AttributeError:

You can access the underlying IDispatch using the
_oleobj_ property, ie elt.onchange._oleobj_.Invoke(......)

Alternately, you can also use FireEvent, which would look
something like this (untested):
elt.FireEvent('onchange',elt.onchange)


Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top