#coding=utf8'''該模式功能是實(shí)現(xiàn)自動(dòng)運(yùn)行sql命令,命令執(zhí)行成功后保存成功截圖并下載結(jié)果文件;運(yùn)行出錯(cuò),則保存錯(cuò)誤截圖;開(kāi)始運(yùn)行保存,設(shè)置命令截圖。該模塊初始化包含如下信息:self.url:用來(lái)保存要訪問(wèn)的網(wǎng)站鏈接self.driver:創(chuàng)建一個(gè)Chrome對(duì)象實(shí)例,并啟動(dòng)Chrome瀏覽器self.driver.maximize_window():窗口最大化self.driver.get(self.url):打開(kāi)網(wǎng)站鏈接self.driver.implicitly_wait(10):設(shè)置隱性等待時(shí)間10秒'''import timefrom selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWait #設(shè)置默認(rèn)編碼格式為utf8import sysreload(sys)sys.setdefaultencoding("utf8")class RunXQL(object): def __init__(self): self.url="" self.driver=webdriver.Chrome() self.driver.maximize_window() self.driver.get(self.url) self.driver.implicitly_wait(10) self.run() def openCmd(self): #設(shè)置命令文件的路徑,使用RunAll.py腳本時(shí),是.//CmdFile//CMD #如果直接運(yùn)行該腳本要更改為:..//CmdFile//CMD filePath=".//CmdFile//CMD" #打開(kāi)文件 cmd=file(filePath,"r+") #讀取文件 cmd=cmd.read() #把文件轉(zhuǎn)換成string類型并解碼 cmds=str(cmd).decode("utf8") #根據(jù)換行符對(duì)命令文件進(jìn)行拆分 #如果不進(jìn)行該項(xiàng)處理,在命令行結(jié)尾會(huì)莫名添加一個(gè)括號(hào)) cmd=cmds.split("/n") #返回命令行列表 return cmd def SetCmdImage(self): try: #獲取當(dāng)前時(shí)間并轉(zhuǎn)換為字符串 now=time.strftime("%Y%m%d%H%M%S") #設(shè)置圖片格式與路徑 #如果直接運(yùn)行該腳本,變成..//SetCmdImage #以下涉及路徑的于此處理方法一樣 fileName=".//SetCmdImage"+"//"+now+"xpl.png" #保存屏幕截圖到相應(yīng)路徑 self.driver.get_screenshot_as_file(fileName) except Exception,e: PRint e def RunErrorImage(self): try: now=time.strftime("%Y%m%d%H%M%S") fileName=".//RunErrorImage"+"//"+now+"xqlErro.png" self.driver.get_screenshot_as_file(fileName) except Exception,e: print e def SuccessImage(self): try: now=time.strftime("%Y%m%d%H%M%S") fileName=".//ExecResutlImage"+"//"+now+"success.png" self.driver.get_screenshot_as_file(fileName) except Exception,e: print e def inputCMD(self,cmd): try: #獲取輸入命令行元素對(duì)象 cmdArea=self.driver.find_element_by_class_name("ace_text-input") #如果元素存在 if cmdArea: #把命令輸入到文本區(qū)域中 cmdArea.send_keys(cmd) #獲取屏幕截圖 self.SetCmdImage() else: print "The element %s not exists!" %("ace_text-input") except Exception,e: print e def runCmd(self): try: #獲取執(zhí)行命令的元素對(duì)象 runEle=self.driver.find_element_by_xpath("//*[@id=/"new-tab-cont/"]/div/div[1]/ul[1]/li[1]/a") #如果元素存在 if runEle: #點(diǎn)擊該元素運(yùn)行命令 runEle.click() else: print "The element %s not exists!" %("exec") except Exception,e: print e def clickDown(self): try: #獲取下載元素對(duì)象 download=self.driver.find_element_by_xpath("//*[@id=/"new-tab-cont/"]/div/div[3]/ul/li[1]/a[1]") #如果元素存在 if download: #點(diǎn)擊該元素執(zhí)行下載操作 download.click() else: print "The element %s not exists!" %("fa fa-download j-dload") except Exception,e: print e def GetResutl(self): try: try: #獲取命令出錯(cuò)或執(zhí)行異常,顯示異常信息的元素對(duì)象 #超過(guò)60秒如果找不到該元素,失敗 text_danger=WebDriverWait(self.driver, 60).until(lambda driver: driver.find_element_by_xpath("//*[@id=/"new-tab-cont/"]/div/div[3]/div/div[1]/pre")) #如果元素存在 if text_danger: #獲取元素的文本信息 errInfro=text_danger.text #獲取屏幕截圖 self.RunErrorImage() print errInfro except Exception,e: pass try: #獲取成功時(shí)顯示的文本信息,超過(guò)300秒元素不存在則失敗 text_success=WebDriverWait(self.driver, 300).until(lambda driver: driver.find_element_by_class_name("text-success")) #如果元素存在 if text_success: #獲取屏幕截圖 self.SuccessImage() print "sql run successs......" #點(diǎn)擊下載按鈕下載運(yùn)行結(jié)果文件 self.clickDown() except Exception,e: print e finally: #最后關(guān)閉瀏覽器 self.driver.close() def run(self): try: cmd=self.openCmd() self.inputCMD(cmd) self.runCmd() self.GetResutl() print "success......" except Exception,e: print e finally: #最后關(guān)閉瀏覽器 self.driver.close() def test(): RunXQL() if __name__=="__main__": test() 完整項(xiàng)目源碼下載鏈接:http://download.csdn.net/download/henni_719/9771946
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注