Example of Python using win32com to simulate browser functionality
- 2020-06-12 09:51:22
- OfStack
This article gives an example of how Python USES win32com to simulate browser functionality. To share for your reference, specific as follows:
# -*- coding:UTF-8 -*-
#!/user/bin/env python
'''
Created on 2010-9-1
@author: chenzehe
'''
import win32com.client
from time import sleep
loginurl='http://passport.cnblogs.com/login.aspx'
loginouturl='http://passport.cnblogs.com/logout.aspx'
username='XXX'
password='XXX'
ie = win32com.client.Dispatch("InternetExplorer.Application")
ie.Visible = 0
ie.Navigate(loginurl)
state = ie.ReadyState
print " Open the login page "
while 1:
state = ie.ReadyState
if state ==4:
break
sleep(1)
print " After the page loads, enter the username and password "
state = None
ie.Document.getElementById("tbUserName").value=username
ie.Document.getElementById("tbPassword").value=password
ie.Document.getElementById("btnLogin").click()
while 1:
state = ie.ReadyState
print state
if state ==4 and str(ie.LocationURL) == "http://home.cnblogs.com/":
break
sleep(1)
print " Log in successfully "
print ' Your nickname is: '
print ie.Document.getElementById('lnk_current_user').title
# Blog garden can only be logged in 1 Time, logout
print ' Log out! '
ie.Navigate(loginouturl)
For more information about Python, please refer to Python Data Structure and Algorithm Tutorial, Python Function Skills Summary, Python String Manipulation Skills Summary, Python Introduction and Advanced Classic Tutorial and Python File and Directory Operation Skills Summary.
I hope this article has been helpful in Python programming.