Python使用win32com实现的模拟浏览器功能

时间:2023-03-10 06:41:45
Python使用win32com实现的模拟浏览器功能
# -*- coding:UTF- -*-
#!/user/bin/env python
'''
Created on --
@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 =
ie.Navigate(loginurl)
state = ie.ReadyState
print "打开登陆页面"
while :
state = ie.ReadyState
if state ==:
break
sleep()
print "页面载入完毕,输入用户名密码"
state = None
ie.Document.getElementById("tbUserName").value=username
ie.Document.getElementById("tbPassword").value=password
ie.Document.getElementById("btnLogin").click()
while :
state = ie.ReadyState
print state
if state == and str(ie.LocationURL) == "http://home.cnblogs.com/":
break
sleep()
print "登陆成功"
print '你的昵称是:'
print ie.Document.getElementById('lnk_current_user').title
#博客园只能登录一次,注销
print '注销!'
ie.Navigate(loginouturl)