您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 工程监理 > Python 操作IE的弹出窗口
Python操作IE的弹出窗口最近和朋友在群里讨论Selenium中,源码中Pyhon关闭IE的弹出窗口失效,后来小编给了个建议是先把焦点设置给一个隐藏的层,然后发送回车的方法去实现它。感觉颇为无奈,同时也发现在PAM30中也是没有实现处理弹出窗口的问题,为此今天在PAM30的基础上新增了下面几个方法:getmsgbox、getmsgboxtext、getmsgboxtitle、closemsgbox、clickmsgboxbutton等等。主要关键点在于getmsgbox方法的实现。defgetmsgbox(self,filter=None):GetthespecifiedPopupmessageboxparameters:[filter]-Onlyreturnelementsthatmatchthisfilterinformat(title=MicrosoftInternetExplorer;text=Areyousuretoclosethewindow;index:=0)Thefiltervaluetomatch.RegularExpressionscanbeusedbystartingthevalwithan!title=!Google;text=!baidu#(Addbyluchenzhi@March30,2010)returns:apopupmessageboxhwndMsgboxHwnd=0MatchTime=0MatchIndex=0try:foriinrange(0,100):MsgboxHwnd=win32gui.FindWindowEx(0,MsgboxHwnd,#32770,None)ifMsgboxHwnd==0:breakifwin32gui.GetParent(MsgboxHwnd)==self._ie.Hwnd:iffilter:valText=Nonefilters=filter.split(;)match=Falseforfinfilters[:]:atts=f.split(=)ifatts[0].lower()==title:valText=win32gui.GetWindowText(MsgboxHwnd)ifatts[0].lower()==text:FirstStaticHwnd=win32gui.FindWindowEx(MsgboxHwnd,0,Static,None)ifwin32gui.GetWindowText(FirstStaticHwnd):valText=win32gui.GetWindowText(FirstStaticHwnd)else:valText=win32gui.GetWindowText(win32gui.FindWindowEx(MsgboxHwnd,FirstStaticHwnd,Static,None))ifatts[0].lower()==index:MatchIndex=int(atts[1])ifvalText==None:match=TruecontinueifvalText!=None:valText=str(valText)valText=valText.strip()valText=valText.lower()wantText=atts[1].lower()ifwantText[0]==!:val=wantText.replace(!,,1)myRE=re.compile(val)m=myRE.match(valText)ifm:match=Trueelse:match=FalsebreakelifvalText==wantText:match=Trueelse:match=Falsebreakelse:returnMsgboxHwndifmatch:MatchTime=MatchTime+1ifMatchTime==MatchIndex+1:returnMsgboxHwndexcept:(ErrorType,ErrorValue,ErrorTB)=sys.exc_info()print(sys.exc_info())traceback.print_exc(ErrorTB)returnNonereturnNone剩下的几个方法包括了:defclickmsgboxbutton(self,filter=None,buttonname=None):clickthespecifiedPopupmessagebox'sbuttonparameters:[filter]-Onlyreturnelementsthatmatchthisfilterinformat(title=MicrosoftInternetExplorer;text=Areyousuretoclosethewindow;index:=0)Thefiltervaluetomatch.RegularExpressionscanbeusedbystartingthevalwithan!title=!Google;text=!baidu[buttonname]-thebuttonnameorindex,itcanusetheRegularExpressionsalso.eg:Yes,!Ye,2#(Addbyluchenzhi@March31,2010)returns:TrueorfalseorNoneMesgboxhwnd=self.getmsgbox(filter)ifMesgboxhwnd==None:returnNoneelse:ButtonHwnd=0iftype(buttonname)==type(1):buttonindex=buttonnamefortinrange(0,buttonindex+1):ButtonHwnd=win32gui.FindWindowEx(Mesgboxhwnd,ButtonHwnd,Button,None)elifbuttonname==None:ButtonHwnd=win32gui.FindWindowEx(Mesgboxhwnd,ButtonHwnd,Button,None)else:foriinrange(0,10):ButtonHwnd=win32gui.FindWindowEx(Mesgboxhwnd,ButtonHwnd,Button,None)ButtonText=win32gui.GetWindowText(ButtonHwnd)ifbuttonname[0]==!:val=buttonname.replace(!,,1)myRE=re.compile(val)m=myRE.match(ButtonText)ifm:breakifButtonHwnd==0:returnNoneelse: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)ifwin32gui.IsWindow(ButtonHwnd)==0:returnTrueelse:returnFalsedefclosemsgbox(self,filter=None):closethespecifiedPopupmessageboxparameters:[filter]-Onlyreturnelementsthatmatchthisfilterinformat(title=MicrosoftInternetExplorer;text=Areyousuretoclosethewindow;index:=0)Thefiltervaluetomatch.RegularExpressionscanbeusedbystartingthevalwithan!title=!Google;text=!baidu#(Addbyluchenzhi@March31,2010)returns:trueorfalse/NoneMesgboxhwnd=self.getmsgbox(filter)ifMesgboxhwnd!=None:win32gui.SendMessage(Mesgboxhwnd,16,1,0)time.sleep(0.6)ifwin32gui.IsWindow(Mesgboxhwnd)==0:returnTrueelse:returnFalseelse:returnNonedefgetmsgboxtitle(self,filter=None):GetthespecifiedPopupmessagebox'stitleparameters:[filter]-Onlyreturnelementsthatmatchthisfilterinformat(title=MicrosoftInternetExplorer;text=Areyousuretoclosethewindow;index:=0)Thefiltervaluetomatch.RegularExpressionscanbeusedbystartingthevalwithan!title=!Google;text=!baidu#(Addbyluchenzhi@March31,2010)returns:apopupmessageboxtitleMesgboxhwnd=self.getmsgbox(filter)ifMesgboxhwnd==None:returnNoneelse:returnwin32gui.GetWindowText(Mesgboxhwnd)defgetmsgboxtext(self,filter=None):GetthespecifiedPopupmessagebox'stextparameters:[filter]-Onlyreturnelementsthatmatchthisfilterinformat(title=MicrosoftInternetExplorer;text=Areyousuretoclosethewindow;index:=0)Thefiltervaluetomatch.RegularExpressionscanbeusedbystartingthevalwithan!title=!Google;text=!baidu#(Addbyluchenzhi@March31,2010)returns:apopupmessageboxtextMesgboxhwnd=self.getmsgbox(filter)ifMesgboxhwnd==None:returnNoneelse:FirstStaticHwnd=win32gui.FindWindowEx(Mesgboxhwnd,0,Static,None)ifwin32gui.GetWindowText(FirstStaticHwnd):returnwin32gui.GetWindowText(FirstStaticHwnd)else:returnwin32gui.GetWindowText(win32gui.FindWindowEx(Mesgboxhwnd,FirstStaticHwnd,Static,None))在实际应用中,代码并不复杂:fromPAM30importPAMIEie=PAMIE(C:\Smart.html)####获取弹出窗口的标题print(ie.getmsgboxtitle())print(ie.getmsgboxtitle(index=0))print(ie.getmsgboxtitle(title=!M;index=0))print(ie.getmsgboxtitle(text=!Waning;index=0)
本文标题:Python 操作IE的弹出窗口
链接地址:https://www.777doc.com/doc-4210411 .html