Pamie模块详解 python操作ie浏览器

此文档使用平台为 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 供大家下载

简单的例子:

  1. import PAM30

  2. ie = PAM30.PAMIE()
  3. ie.navigate('www.google.com')
  4. ie.setTextBox('q','PAMIE')
  5. ie.clickButton('btnG')
复制代码

      但是我们实际使用中,实际上会碰到许多的控件。如,input ,radio ,checkbox.

下面针对不同控件给出函数列表, 大家看名字大概就应该明白是干什么的. 权当手册使用吧,呵呵 :

状态控制
       实际应用中,需要考虑IE的状态,比如访问一个网页,随即进行操作,在网页未下载完成的情况下,容易出错
       PAMIE 则考虑到下面的情况     
  1. _wait()  : 等待整个页面下载完成
  2. _frameWait() :  等待一个frame下载完成
  3. _docGetReadyState:  获取文档对象的状态
复制代码

TextArea
  1. getTextArea (name):    获取一个textarea,可能使用 id,name 或者 index
  2. getTextAreaValue(name, attribute):   获取一个textarea的属性值
  3. getTextAreasValue() 获取所有textarea
  4. setTextArea(name): 给textarea赋值
  5. textAreaExists(name): 查看指定名称的textarea 是否存在
复制代码

Input
  1. getTextBox(name):
  2. getTextBoxValue(name, attribute):
  3. getTextBoxes():
  4. getTextBoxesValue()
  5. setTextBox( name, value):
  6. getInputElements():
复制代码

Button
  1. buttonExists(self, name):  检查一个button是否存在
  2. clickButton(self, name):   点击一个按钮, name 可以是 name,id,index甚至是value属性的值
  3. clickButtonImage(self, name):  同上,点击一个图片按钮
复制代码

Radio
  1. getRadioButton(name):
  2. def getRadioButtonSelected(name):
  3. getRadioButtonValues(name):
  4. getRadioButtons():
复制代码

CheckBox
  1. checkBoxExists(self, name): 检查一个单选框是否存在
复制代码

ListBox
  1. getListBox(name):
  2. getListBoxItemCount(name):
  3. getListBoxOptions(name):
  4. getListBoxSelected(name):
  5. getListBoxValue(name, attribute):
  6. listBoxUnSelect(name, value):
  7. selectListBox(name, value):
复制代码

Image
  1. getImage( name):
  2. getImageValue(name, attribute):
  3. getImages():
  4. getImagesValue( attribute):
  5. imageExists(name):
复制代码

form
  1. formExists( name):
  2. getForm( name=None):
  3. getFormValue( name, attribute):
  4. getFormVisibleControlNames( name=None):
  5. getForms():
  6. getFormsValue( attribute):
复制代码

a
  1. clickHiddenLink( name):
  2. getLink( name):
  3. getLinkValue( name, attribute):
  4. getLinks( filter=None):
复制代码

table 
  1. getTable( name):
  2. getTableData( name):
  3. getTableRowIndex( name, row):
  4. getTableText(tableName,rownum,cellnum, frameName=None):
  5. getTables( filter=None):
  6. tableCellExists( tableName, cellText):
  7. tableExists( name):
  8. tableRowExists( name, row):
复制代码

div
  1. divExists( name):
  2. getDiv( name):
  3. getDivValue( name, attribute):
  4. getDivs():
  5. getDivsValue( attribute):
复制代码

通用 Element
  1. clickElement( element):  点击一个节点。
  2. clickHiddenElement( element):
  3. findElement( tag, attributes, val, elementList=None):
  4. findElementByIndex( tag, indexNum, filter=None, elementList=None):
  5. findText( text):
  6. fireElementEvent( tag, controlName, eventName):
  7. textFinder(text):
  8. getElementChildren( element, all=True):
  9. getElementParent( element):
  10. getElementValue( element, attribute):
  11. getElementsList( tag, filter=None, elementList=None):
复制代码

操作类函数:
      用于控制IE的动作
  1. navigate( url):
  2. changeWindow( wintext):  转而控制另外一个窗体对象
  3. pause( string = "Click to Continue test"):
  4. goBack(self):
  5. findWindow( title, indexNum=1):
  6. closeWindow( title=None):
  7. refresh(self):
  8. resize( iWidth, iHeight):
  9. quit(self):
复制代码

信息资源类:
       获取当前IE的属性
  1. getIE(self):
  2. getPageText(self):
  3. locationURL(self):
  4. outerHTML(self):
  5. randomString( length):




http://www.cnblogs.com/babykick/archive/2011/06/10/2077330.html

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值