Python 操作IE的弹出窗口.doc_第1页
Python 操作IE的弹出窗口.doc_第2页
Python 操作IE的弹出窗口.doc_第3页
Python 操作IE的弹出窗口.doc_第4页
Python 操作IE的弹出窗口.doc_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Python 操作IE的弹出窗口最近和朋友在群里讨论Selenium中,源码中Pyhon关闭IE的弹出窗口失效,后来小编给了个建议是先把焦点设置给一个隐藏的层,然后发送回车的方法去实现它。感觉颇为无奈,同时也发现在PAM30中也是没有实现处理弹出窗口的问题,为此今天在PAM30的基础上新增了下面几个方法:getmsgbox、getmsgboxtext、getmsgboxtitle、closemsgbox、clickmsgboxbutton等等。主要关键点在于getmsgbox方法的实现。def getmsgbox(self, filter = None): Get the specifiedPopupmessage box parameters: filter - Only return elements that match this filter in format (title=Microsoft Internet Explorer;text=Are you sure to close the window;index:=0) The filter value to match. Regular Expressions can be used by starting the val with an ! title=!Google;text=!baidu #(Add by luchenzhi March 30,2010) returns: a popup message box hwnd MsgboxHwnd = 0 MatchTime = 0 MatchIndex = 0 try: for i in range(0,100) : MsgboxHwnd=win32gui.FindWindowEx(0,MsgboxHwnd,#32770,None) if MsgboxHwnd = 0 : break if win32gui.GetParent(MsgboxHwnd) = self._ie.Hwnd: if filter: valText = None filters = filter.split(;) match = False for f in filters: atts = f.split(=) if atts0.lower() = title: valText = win32gui.GetWindowText(MsgboxHwnd) if atts0.lower() = text: FirstStaticHwnd = win32gui.FindWindowEx(MsgboxHwnd,0,Static,None) if win32gui.GetWindowText(FirstStaticHwnd): valText = win32gui.GetWindowText(FirstStaticHwnd) else: valText = win32gui.GetWindowText(win32gui.FindWindowEx(MsgboxHwnd,FirstStaticHwnd,Static,None) if atts0.lower() = index : MatchIndex=int(atts1) if valText = None : match = True continue if valText != None: valText = str(valText) valText = valText.strip() valText = valText.lower() wantText = atts1.lower() if wantText0 = !: val = wantText.replace( !, , 1) myRE = pile(val) m = myRE.match(valText) if m: match=True else: match=False break elif valText = wantText : match = True else: match = False break else: return MsgboxHwnd if match: MatchTime = MatchTime+1 if MatchTime = MatchIndex+1: return MsgboxHwnd except: (ErrorType,ErrorValue,ErrorTB)=sys.exc_info() print (sys.exc_info() traceback.print_exc(ErrorTB) return None return None剩下的几个方法包括了:def clickmsgboxbutton(self, filter = None, buttonname = None): click the specifiedPopupmessage boxs button parameters: filter - Only return elements that match this filter in format (title=Microsoft Internet Explorer;text=Are you sure to close the window;index:=0) The filter value to match. Regular Expressions can be used by starting the val with an ! title=!Google;text=!baidu buttonname - the button name or index, it can use the Regular Expressions also. eg : Yes,!Ye,2 #(Add by luchenzhi March 31,2010) returns:True or false or None Mesgboxhwnd = self.getmsgbox(filter) if Mesgboxhwnd = None : return None else: ButtonHwnd=0 if type(buttonname) = type(1) : buttonindex=buttonname for t in range(0,buttonindex+1): ButtonHwnd=win32gui.FindWindowEx(Mesgboxhwnd,ButtonHwnd,Button,None) elif buttonname = None : ButtonHwnd=win32gui.FindWindowEx(Mesgboxhwnd,ButtonHwnd,Button,None) else: for i in range(0,10) : ButtonHwnd=win32gui.FindWindowEx(Mesgboxhwnd,ButtonHwnd,Button,None) ButtonText=win32gui.GetWindowText(ButtonHwnd) if buttonname0 = !: val = buttonname.replace( !, , 1) myRE = pile(val) m = myRE.match(ButtonText) if m: break if ButtonHwnd =0: return None else: win32gui.SendMessage(ButtonHwnd,513,1,0) win32gui.SendMessage(ButtonHwnd,514,0,0) win32gui.SendMessage(ButtonHwnd,513,1,0) win32gui.SendMessage(ButtonHwnd,514,0,0) time.sleep(0.6) if win32gui.IsWindow(ButtonHwnd) = 0 : return True else: return Falsedef closemsgbox(self, filter = None): close the specified Popup message box parameters: filter - Only return elements that match this filter in format (title=Microsoft Internet Explorer;text=Are you sure to close the window;index:=0) The filter value to match. Regular Expressions can be used by starting the val with an ! title=!Google;text=!baidu #(Add by luchenzhi March 31,2010) returns: true or false /None Mesgboxhwnd = self.getmsgbox(filter) if Mesgboxhwnd != None : win32gui.SendMessage(Mesgboxhwnd,16,1,0) time.sleep(0.6) if win32gui.IsWindow(Mesgboxhwnd) = 0 : return True else: return False else: return Nonedef getmsgboxtitle(self, filter = None): Get the specified Popup message boxs title parameters: filter - Only return elements that match this filter in format (title=Microsoft Internet Explorer;text=Are you sure to close the window;index:=0) The filter value to match. Regular Expressions can be used by starting the val with an ! title=!Google;text=!baidu #(Add by luchenzhi March 31,2010) returns: a popup message box title Mesgboxhwnd = self.getmsgbox(filter) if Mesgboxhwnd = None : return None else: return win32gui.GetWindowText(Mesgboxhwnd) def getmsgboxtext(self, filter = None): Get the specified Popup message boxs text parameters: filter - Only return elements that match this filter in format (title=Microsoft Internet Explorer;text=Are you sure to close the window;index:=0) The filter value to match. Regular Expressions can be used by starting the val with an ! title=!Google;text=!baidu #(Add by luchenzhi March 31,2010) returns: a popup message box text Mesgboxhwnd = self.getmsgbox(filter) if Mesgboxhwnd = None : return None else: FirstStaticHwnd = win32gui.FindWindowEx(Mesgboxhwnd,0,Static,None) if win32gui.GetWindowText(FirstStaticHwnd): return win32gui.GetWindowText(FirstStaticHwnd) else: return win32gui.GetWindowText(win32gui.FindWindowEx(Mesgboxhwnd,FirstStaticHwnd,Static,None)在实际应用中,代码并不复杂:from PAM30 import PAMIEie=PAMIE(C:Smart.html)#获取弹出窗口的标题pri

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论