此文档使用平台为 cPAMIE Build 2.0,和之前的版本有明显的差别,具体可直接看cPAMIE.py 源码
下面是一些常用的方法
ie.navigate('http://blog.alexa.cn') 用来访问一个链接。
ie.linkClick('linkname') 打开这个页面中的一个连接 参数: name或 id
ie.textBoxSet('labels','python') 设置一个文本域的值。第一个参数为文本域的名称,第二个参数为要输入的值,
ie.buttonClick('btnA') 用来模拟点击按钮 参数是按钮的名字
ie.findText(unicode("登录", "cp936")) 查找文本 如:
if (True == ie.findText(unicode("非法登录", "cp936"))):
print "非法登录!"
else:
print "登录成功!"
ie.quit() -- 关闭打开的ie
ie.goBack() -- 后退
ie.refresh() -- 刷新
ie.buttonGetValue('wp-submit','value') 得到一个无素的其它值,第一个参数为你知道的元素属性如:id, name, value 等,第二个参数为你要得到的这个元素的其它值,这样如果你知道name,就可得到id,value等,如果知道name就可知道id,value.....
ie.buttonClick('down') 这个方法的参数可以是name,ID,VALUE等,注:NAME和VALUE为中文时记着转码如:unicode("断线", "cp936")
ie.elementsGetList(tag, [filter], ) 返回一个过滤后的元素列表
符几个例子:
这个是将文章提交给GOOGLE收藏夹的脚本,用法 postgoogle.py http://blog.alexa-pro.cn/index.php
import cPAMIE,re,string,time,sys
ie= cPAMIE.PAMIE()
args = sys.argv
ie.navigate(args[1])
Title=ie.locationName().strip()
Title=Title.replace('GGclub','').replace(' Blog Archive ','')
ie.navigate('https://www.google.com/accounts/Logout?continue=http://www.google.com/intl/zh-CN/&hl=zh-CN')
ie.navigate('https://www.google.com/accounts/ServiceLogin?hl=zh-
CN&continue=http://www.google.com/bookmarks/mark%3Fop%3Dadd%26bkmk%3Dhttp%253A%252F%252Fblog.alexa-pro.cn%252F%
26title%3DGGclub&nui=1&service=bookmarks')
ie.textBoxSet('Email','yingguang08@gmail.com')
ie.textBoxSet('Passwd','*******')
ie.buttonClick('signIn')
ie.textBoxSet('title',Title)
ie.textBoxSet('bkmk',args[1])
ie.textBoxSet('labels','python')
ie.textBoxSet('annotation','python linux')
ie.buttonClick('btnA')
解释:
先执行退出操作,以免现在是登陆状态 而报错
ie.navigate('https://www.google.com/accounts/Logout?continue=http://www.google.com/intl/zh-CN/&hl=zh-CN')
下面是一个为做网赚的朋友做的小脚本,实现了
在登陆状态下http://www4.bux.to , 列出要点击的连接,进行点击操作,每个点击后持续40秒后再点下一个
import cPAMIE,re,time
ie= cPAMIE.PAMIE()
Url='http://www4.bux.to/surf.php'
ie.navigate(Url)
cc=ie.outerHTML()
cc = re.findall('''target=_blank>(.*?)</A>''',cc)
for click in cc:
print click
ie.linkClick(click)
time.sleep(40)
AM30, 操作IE的好帮手
在python下,大家可能需要操作IE访问网站。也许有人要问了,我们有很多优秀的网站访问插件呀,比如之前提到的 mechanize
其实大家仔细看 mechanize 就知道了,它是不支持 js 操作的。虽然我们很多时候可以通过模拟 js 的业务逻辑来模拟。但是也有极端的情况下是无法做到的。比如,你需要向一个网站投递新闻,而同时需要输入验证码。而且这个新闻呢,也必须注意格式,也就是 说投递者本人必须看到内容,同时可以做修改。那这样的情况,mechanize就无能为力了。
废话不多说,进入正题。让我来介绍今天的主角 PAMIE.
PAMIE使用 依赖于 win32com 这个组件。在此基础上操作IE. 附件中提供 win32com 及 PAMIE 供大家下载
简单的例子:
- import PAM30
- ie = PAM30.PAMIE()
- ie.navigate('www.google.com')
- ie.setTextBox('q','PAMIE')
- ie.clickButton('btnG')
但是我们实际使用中,实际上会碰到许多的控件。如,input ,radio ,checkbox.
下面针对不同控件给出函数列表, 大家看名字大概就应该明白是干什么的. 权当手册使用吧,呵呵 :
状态控制
实际应用中,需要考虑IE的状态,比如访问一个网页,随即进行操作,在网页未下载完成的情况下,容易出错
PAMIE 则考虑到下面的情况
- _wait() : 等待整个页面下载完成
- _frameWait() : 等待一个frame下载完成
- _docGetReadyState: 获取文档对象的状态
TextArea
- getTextArea (name): 获取一个textarea,可能使用 id,name 或者 index
- getTextAreaValue(name, attribute): 获取一个textarea的属性值
- getTextAreasValue() 获取所有textarea
- setTextArea(name): 给textarea赋值
- textAreaExists(name): 查看指定名称的textarea 是否存在
Input
- getTextBox(name):
- getTextBoxValue(name, attribute):
- getTextBoxes():
- getTextBoxesValue()
- setTextBox( name, value):
- getInputElements():
Button
- buttonExists(self, name): 检查一个button是否存在
- clickButton(self, name): 点击一个按钮, name 可以是 name,id,index甚至是value属性的值
- clickButtonImage(self, name): 同上,点击一个图片按钮
Radio
- getRadioButton(name):
- def getRadioButtonSelected(name):
- getRadioButtonValues(name):
- getRadioButtons():
CheckBox
- checkBoxExists(self, name): 检查一个单选框是否存在
ListBox
- getListBox(name):
- getListBoxItemCount(name):
- getListBoxOptions(name):
- getListBoxSelected(name):
- getListBoxValue(name, attribute):
- listBoxUnSelect(name, value):
- selectListBox(name, value):
Image
- getImage( name):
- getImageValue(name, attribute):
- getImages():
- getImagesValue( attribute):
- imageExists(name):
form
- formExists( name):
- getForm( name=None):
- getFormValue( name, attribute):
- getFormVisibleControlNames( name=None):
- getForms():
- getFormsValue( attribute):
a
- clickHiddenLink( name):
- getLink( name):
- getLinkValue( name, attribute):
- getLinks( filter=None):
table
- getTable( name):
- getTableData( name):
- getTableRowIndex( name, row):
- getTableText(tableName,rownum,cellnum, frameName=None):
- getTables( filter=None):
- tableCellExists( tableName, cellText):
- tableExists( name):
- tableRowExists( name, row):
div
- divExists( name):
- getDiv( name):
- getDivValue( name, attribute):
- getDivs():
- getDivsValue( attribute):
通用 Element
- clickElement( element): 点击一个节点。
- clickHiddenElement( element):
- findElement( tag, attributes, val, elementList=None):
- findElementByIndex( tag, indexNum, filter=None, elementList=None):
- findText( text):
- fireElementEvent( tag, controlName, eventName):
- textFinder(text):
- getElementChildren( element, all=True):
- getElementParent( element):
- getElementValue( element, attribute):
- getElementsList( tag, filter=None, elementList=None):
操作类函数:
用于控制IE的动作
- navigate( url):
- changeWindow( wintext): 转而控制另外一个窗体对象
- pause( string = "Click to Continue test"):
- goBack(self):
- findWindow( title, indexNum=1):
- closeWindow( title=None):
- refresh(self):
- resize( iWidth, iHeight):
- quit(self):
信息资源类:
获取当前IE的属性
- getIE(self):
- getPageText(self):
- locationURL(self):
- outerHTML(self):
- randomString( length):