1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 import time 4 5 time_start=time.time() 6 7 8 #default setting of firefox 9 import os 10 fp = webdriver.FirefoxProfile() 11 fp.set_preference("browser.download.folderList",0)#0代表下载到浏览器默认下载路径 12 fp.set_preference("browser.download.manager.showhenStarting",False) 13 fp.set_preference("browser.download.dir",os.getcwd()) 14 fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/vnd.ms-excel")#下载文件类型 15 16 17 driver = webdriver.Firefox(firefox_profile = fp) 18 19 20 #login 21 driver.get(‘http://xxxxxxxx/login‘) 22 driver.find_element(By.ID, "txtUserID").send_keys("xxxx") 23 driver.find_element(By.ID, "txtPwd").send_keys("xxxx") 24 driver.find_element(By.ID, "submit_btn").click() 25 26 27 #open aim tab 28 import time 29 driver.find_element(By.CSS_SELECTOR, ".panel:nth-child(3) .panel-title").click() 30 31 32 from selenium.webdriver.support.wait import WebDriverWait 33 from selenium.webdriver.support import expected_conditions as EC 34 def shortSleep(aimLocator): 35 WebDriverWait(driver, 10, 0.1).until(EC.presence_of_element_located(aimLocator)) 36 37 38 def fakeSleep(func,*args,timeout = 3,tik = 0.1): 39 import time 40 time_start=time.time() 41 while timeout>0: 42 time.sleep(tik) 43 try: 44 func(*args) 45 timeout = 0 46 except: 47 pass 48 time_end = time.time() 49 print(‘time cost‘,round(time_end-time_start,2),‘s‘) 50 51 52 #btnC1Locator = (By.ID, "_easyui_tree_12") 53 def locAimTreeNode(): 54 driver.find_element(By.ID, "_easyui_tree_12").click() 55 driver.find_element(By.CSS_SELECTOR, "#\\_easyui_tree_18 > .tree-title").click() 56 57 58 fakeSleep(locAimTreeNode) 59 print("locAimTreeNode-end") 60 61 62 def switchFrame(): 63 driver.switch_to.frame(1) 64 65 66 fakeSleep(switchFrame) 67 print("switchFrame-end") 68 69 70 #switch to new tab 71 def searchMounthSum(): 72 import editElement 73 date_show_text = driver.find_element(By.CSS_SELECTOR, ".item > .text") 74 editElement.setText(driver,date_show_text,"本月") 75 #change search days 76 date_inner_value = driver.find_element(By.XPATH, "//*[@id=‘MySearchPanel‘]/div/div[2]/div[1]/span[2]/a/input") 77 import datetime 78 startDate = datetime.datetime.now().strftime(‘%Y-%m-01‘) 79 currentDate = datetime.datetime.now().strftime(‘%Y-%m-%d‘) 80 searchStr = startDate+‘@‘+currentDate 81 editElement.setAttribute(driver,date_inner_value,"value",searchStr) 82 driver.find_element(By.CSS_SELECTOR, ".tbar > div:nth-child(1) > button:nth-child(1) > span:nth-child(2)").click() 83 84 85 fakeSleep(searchMounthSum) 86 print("searchMounthSum-end") 87 88 89 def tableFinished(): 90 rowsCount = driver.find_elements(By.XPATH, "/html/body/div[1]/div/div/div[3]/div/div/div[4]/div[1]/div[2]/div[2]/table/tbody/tr") 91 if len(rowsCount)<1: 92 raise RuntimeError(‘not ready yet‘) 93 fakeSleep(tableFinished) 94 print("tableFinished-end") 95 96 97 #export 98 def export(): 99 import editElement 100 ffNeedMakeThisItemVis =driver.find_element(By.CSS_SELECTOR, "div.sciyon-combo:nth-child(4)") 101 editElement.setAttribute(driver,ffNeedMakeThisItemVis,"style","overflow: auto; width: 100px; left: 1210.6px; top: 622.2px; display: true;") 102 driver.find_element(By.CSS_SELECTOR, "div.sciyon-combo:nth-child(4) > div:nth-child(1)").click() 103 fakeSleep(export) 104 print("export-end") 105 106 107 #check meg box 108 import win32gui 109 import win32con 110 import win32api 111 def click(xPos,yPos): 112 # 将鼠标移动到坐标处 113 win32api.SetCursorPos((xPos, yPos)) 114 # 左点击 115 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 200, 200, 0, 0) 116 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 200, 200, 0, 0) 117 118 119 def catchPrintWindow(): 120 dialog = win32gui.FindWindow(‘MozillaDialogClass‘,None) # 对话框 121 windowName = win32gui.GetWindowText(dialog) 122 if windowName == ‘‘: 123 raise RuntimeError(‘not catched till now‘) 124 print("窗口名称:"+windowName) 125 #print("btn名称:"+win32gui.GetWindowText(btnhld)) #发现无法取到下级控件,无句柄 126 #(643, 201)-(1201, 596), 558x395 127 win32gui.SetWindowPos(dialog, win32con.HWND_TOPMOST, 0,0,420,400, win32con.SWP_SHOWWINDOW)#偷懒,使用按键精灵录制的绝对坐标 128 time.sleep(1) 129 click(64,254)#问题点:没有考虑win10内容缩放、屏幕分辨率,缩放这个估计有难度,可能需要管理员权限 130 click(230,359) 131 fakeSleep(catchPrintWindow) 132 print("catchPrintWindow-end") 133 134 135 print("Script End") 136 time_end=time.time() 137 print(‘time cost‘,time_end-time_start,‘s‘)
每个月要打印本月考勤记录,加上想学习下selenium用于以后的测试,就先练练手。主要思路是:自动登录erp,自动点击到相关页面,修改部分元素属性方便查询,(顺便通过这种形式绕过我们erp在火狐上的一些无法点击的BUG),然后点击下载,修改firefox默认下载路径,捕获弹出的下载窗口(挣扎了一下并没有达到静默下载。。),根据坐标位置定位radio和btn控件(找不到句柄),完成。第二步的导出文件的解析在这几天之前基本已经完成,获取再把提交报销流程完成就可以实现 双击完了贴发票的 乐趣了。(估计不会完成了,暂时还有好多需要学习的,到此为止吧。手动2下算了)
本次编写过程中值得自己记下来的点:
- 查找元素可以用插件rec Selenium IDE,比较省时间
- 元素定位有时候因为渲染较慢,需要等待
- python很容易可将function 当作另一个function的参数(本次应用在,如果抛异常,就隔0.1秒再来,最多给10秒机会)
- 使用spy++先查找窗体的类名,窗体名称不太稳,如本次编写,发现名称竟是下载文件名
- 窗体句柄容易得,控件则较难(发现之前winform程序很好找,用spy++也可,但是现在的程序越来越难了)
最后因为水平有限,目前以实现为主,对于注释、封装、测试(不断对整个脚本F5)还需要后续学习优化。
原文地址:https://www.cnblogs.com/ngwfp/p/11732093.html
时间: 2024-10-07 06:55:31