win32com and internet explorer

chandan chandan_mathur at hotmail.com
Sat Jul 6 00:05:59 EDT 2002


alrite..., it works.., i don't really need the header part.. so i
didn't bother. Had some trouble with saving the file to disk..., but
eventually found out that i had to convert them to unicode and then
ignore special characters.
this is what i have so far...
import win32com.client,time,string
# Get the browser object
ie = win32com.client.DispatchEx("InternetExplorer.Application")
ie.Visible = 0
ie.Navigate("http://www.google.com")
time.sleep(5)
text = ie.Document.body.innerHTML
text = unicode(text)
text = text.encode('ascii','ignore')

filename = "c:/test.html"
output_file = open(filename,'w')
output_file.write(text)
output_file.close()
ie.close()

 But i am stuck with the submiting username and password through the
form.. i tried to adapt your code to my https..., this is what i found
out.. i am pasting the HTML source from the login page that redirects
it to https
(http://www.fool.com/Login.asp") :

<form method="POST" action="https://www.fool.com/Login.asp?numtries=1&loginredirect=http%3A%2F%2Fmy%2Efool%2Ecom%2F%3Fref%3Dlogin"
name="xyz">
<div align="center">
<table width="400" class="tcmods" cellpadding="2" cellspacing="0"
border="0" bgcolor="#e5e5e5">
<tr>
<td align="right" valign="middle" class="tctext" width="50%">
<b>User Name or Email Address:</b>
</td>
<td valign="top">
<input class="tcforms" type=text name="uf" size=20 maxlength="64" 
value="westlab1">
</td>
</tr>

<tr>
<td align="right" valign="middle" class="tctext">
<b>Password:</b>
</td>
<td align="left" valign="top">
<input class="tcforms" TYPE="Password" NAME="gf" SIZE="20"
maxlength="22" value="">...and so on

I am guessing, name:value pairs are sent in the post request.. and
therfore.. my code must look something like this..

ie.Navigate("http://www.fool.com/login.asp")
doc = ie.Document
doc.forms(0).uf.value = "username" #from name = "uf" refer HTML above
time.sleep(1)                            
doc.tcforms.gf.value = "Password"    #from name = "gf" refer HTML
above
time.sleep(1)
doc.forms(0).submit()      # Sends the info.

Am i doing this right? i get some crazy exception

THanx for your time and help :)
Chandan
P.S , what does HTH mean?
Also, the links to MSDN give me 404 error...

"Jose Correia" <correia_j at hotmail.com> wrote in message news:<Gp7V8.17174$PN5.463793 at news1.telusplanet.net>...
> Don't know how to get the entire page saved to disk, but hopefully this gets
> you a little closer to your goal if you're trying to get at the info on the
> page.
> 
> >>> from win32com.client import Dispatch
> >>> ie = Dispatch("InternetExplorer.Application")
> >>> ie.Navigate("http://www.google.com")
> >>> ie.Visible=1
> >>> ie.Document.body.innerHTML
> '<CENTER>\015\012<TABLE cellSpacing=0 ........'
> 
> ... this gives you all the html between the BODY tag in a string, you just
> don't get the Header bit.  I'm sure there's a way to get it all but I
> haven't been able to find it yet.  If you find it let me know.
> 
> If you're trying to submit info on the page you need to look at the source
> to see the object names.
> For example, a secure site I use has a form (called frmLogin) to enter an
> account number (field called: Account)
> and password (field called: Password).
> 
> The code I use looks like this:
> doc = ie.Document
> doc.forms(0).Account.value = "987654321"        # Referenced as the first (0
> based count) form on the page.
> sleep(0.5)                            # Otherwise it seems to run too fast
> and get confused :-(
> doc.frmLogin.Password.value = "some_password"    #Referenced by the form's
> name in the HTML.
> sleep(0.5)
> doc.forms(0).submit()        # Sends the info.
> sleep(1)
> 
> You can check if it's finished processing by checking if
> ie.ReadyState == 4         and
> doc.readyState == "complete"
> 
> For further info on the MS stuff try:
> http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/ref
> erence/objects/obj_document.asp
> 
> and
> http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrows
> er/reference/ifaces/IWebBrowser2/IWebBrowser2.asp
> 
> 
> HTH,
> 
> Jose



More information about the Python-list mailing list