The following code creates an IE browser, navigates to Google, and then
clicks on the 'News' link.
----- code -----
ie = win32com.client.DispatchEx('InternetExplorer.Application')
ie.Visible = 1
ie.Navigate('www.google.com')
time.sleep(10)
# show a '.' access fairly deep into the page's DOM (to the 'News' link)
print 'navigate'
anchor =
ie.Document.all.tags('BODY').item(0).all.tags('DIV').item(0).all.tags('NOBR'
).item(0).all.tags('DIV').item(3).all.tags('A').item(0)
print 'click'
anchor.click()
----- end code -----
Interestingly, the rather long statement 'anchor =' works (yes I know there
are easier ways to get to the desired tag).
I assume (my Python is not so good that I KNOW) that it works since the
expression is evaluated left to right and each method returns an object
that, in turn, has the appropriate methods for subsequent evaluation.
Question 1:Is this the case?
While the DOM's navigation methods, see
http://msdn2.microsoft.com/en-us/library/ms533050.aspx are powerful, there
are a number of instances where I'd like to enhance them. I'm considering
the possibility of 'inserting' a method into the object's class dictionary
that does so.
A bit of experimentation shows that this is possible for any given DOM
object. However, there are a bunch of these. The ones I've looked at
derive from win32com.client.DispatchBaseClass.
Question 2: Do ALL the IE COM objects derive from this class? How can I
know for sure without exhaustive examination?
A bit more experimentation yielded the following test code:
----- code -----
# test 'injecting' functions into DispatchBaseClass
import win32com.client
import time
class yamieX(object):
def __init__(self, wait = 5):
self.wait = wait
self.ie = win32com.client.DispatchEx('InternetExplorer.Application')
self.ie.Visible = 1
self.ie.Navigate('www.google.com')
time.sleep(wait)
# inject DOM extensions
win32com.client.DispatchBaseClass.__dict__['tag']=tag
win32com.client.DispatchBaseClass.__dict__['clickWait']=clickWait
def GoBack(self):
self.ie.GoBack()
time.sleep(self.wait)
def Wait(self):
time.sleep(self.wait)
def _body(self):
return self.ie.Document.all.tags('BODY').item(0)
body = property(_body, None, None, None)
def tag(object, str, index=0):
return object.all.tags(str).item(index)
def clickWait(object, yamieX):
object.click()
yamieX.Wait()
####################
# try the magic out
####################
yie = yamieX()
print "show a '.' access fairly deep into the page's DOM (to the 'News'
link)"
print 'navigate and click'
anchor =
yie.ie.Document.all.tags('BODY').item(0).all.tags('DIV').item(0).all.tags('N
OBR').item(0).all.tags('DIV').item(3).all.tags('A').item(0)
anchor.click()
yie.Wait()
print 'show we got to http://news.google.com/nwshp?tab=wn LocationURL=', \
yie.ie.LocationURL == u'http://news.google.com/nwshp?tab=wn'
print
print 'go back'
yie.GoBack()
print
print "do it again using tag extension"
anchor =
yie.body.tag('DIV').tag('NOBR').tag('DIV',3).tag('A').clickWait(yie)
#anchor.click()
yie.Wait()
print 'show we got to http://news.google.com/nwshp?tab=wn LocationURL=', \
yie.ie.LocationURL == u'http://news.google.com/nwshp?tab=wn'
print 'success'
----- end code -----
All this appears to work in that it creates the expected output. But I'm a
bit concerned in that I'm poking Python and win32com rather hard.
Question 3: Any advice or observations on how 'safe' this might be?
Thanks for any feedback.
Regards,
Richard
_______________________________________________
Python-win32 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-win32