無人工幹預地自動上傳附件,人工幹預上傳附件
<html><head> <title>上傳檔案</title> <meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"></head><body> <form enctype = "multipart/form-data" action = "parse_file.jsp" method="post"> <p>Browse for a file to upload:</p> <input id = "file" name="file" typr="file"></input> <br/><br/> <input type="submit" id="filesubmit" value="SUBMIT"></input> </form></body></html>
1、使用webdriver的send_keys方法上傳檔案
#!usr/bin/env python #-*- coding:utf-8 -*- """ @author: sleeping_cat@Contact : zwy24zwy@163.com """ #無人工幹預地自動上傳附件#使用webdriver的send_keys方法上傳檔案from selenium import webdriverimport unittestimport timeimport tracebackfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.common.exceptions import TimeoutException,NoSuchElementExceptionclass TestDemo(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def test_uploadFileBySendKeys(self): url = "e:\\uploadFile.html" self.driver.get(url) try: wait = WebDriverWait(self.driver,10,0.2)#建立一個顯示等待對象 wait.until(EC.element_to_be_clickable((By.ID,'file')))#顯示等待判斷被測試頁面上的上傳檔案按鈕是否處於可被單擊狀態 except TimeoutException as e: print(traceback.print_exc()) except NoSuchElementException as e: print(traceback.print_exc()) except Exception as e: print(traceback.print_exc()) else: fileBox = self.driver.find_element_by_id('file') fileBox.send_keys('c:\\test.txt')#在檔案上傳框的路徑框裡輸入要上傳的檔案路徑C://test.txt time.sleep(3) fileSubmitButton = self.driver.find_element_by_id('filesubmit') fileSubmitButton.click() def tearDown(self): self.driver.quit()if __name__ == '__main__': unittest.main()
2、類比鍵盤操作,實現上傳檔案
#!usr/bin/env python #-*- coding:utf-8 -*- """ @author: sleeping_cat@Contact : zwy24zwy@163.com """ #無人工幹預地自動上傳附件#類比鍵盤操作,實現上傳檔案from selenium import webdriverimport unittestimport timeimport tracebackimport win32clipboard as wimport win32apiimport win32confrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.common.exceptions import TimeoutException,NoSuchElementException#用於設定剪貼簿內容def setText(aString): w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_UNICODETEXT,aString) w.CloseClipboard()#鍵盤按鍵映射字典VK_CODE = { 'enter':0x0D, 'Ctrl':0x11, 'V':0x56}#鍵盤鍵按下def keyDown(keyName): win32api.keybd_event(VK_CODE[keyName],0,0,0)#鍵盤鍵抬起def keyUp(keyName): win32api.keybd_event(VK_CODE[keyName],0,win32con.KEYEVENTF_KEYUP,0)class TestDemo(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def test_uploadFileByKeyboard(self): url = 'e://uploadFile.html' self.driver.get(url) try: wait = WebDriverWait(self.driver,10,0.2) wait.until(EC.element_to_be_clickable((By.ID,'file'))) except TimeoutException as e: print(traceback.print_exc()) except NoSuchElementException as e: print(traceback.print_exc()) except Exception as e: print(traceback.print_exc()) else: setText('c:\\test.txt')#將即將要上傳的檔案名稱及路徑設定到剪貼簿中 self.driver.find_element_by_id('file').click() time.sleep(3) #類比鍵盤按下Ctrl+V keyDown('Ctrl') keyDown('V') #類比鍵盤釋放Ctrl+V keyUp('V') keyUp('Ctrl') time.sleep(1) keyDown('enter') keyUp('enter') time.sleep(2) fileSubmitButton = self.driver.find_element_by_id('filesubmit') time.sleep(2) fileSubmitButton.click() def tearDown(self): self.driver.quit()if __name__ == '__main__': unittest.main()
3、使用第三方工具AutoIt上傳檔案
#!usr/bin/env python #-*- coding:utf-8 -*- """ @author: sleeping_cat@Contact : zwy24zwy@163.com """ #無人工幹預地自動上傳附件#使用第三方工具AutoIt上傳檔案from selenium import webdriverimport unittest,time,os,tracebackfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.common.exceptions import TimeoutException,NoSuchElementExceptionclass TestDemo(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def test_uploadFileByAutoIt(self): url = 'e:\\uploadFile.html' self.driver.get(url) try: wait = WebDriverWait(self.driver,10,0.2) wait.until(EC.element_to_be_clickable((By.ID,'file'))) except TimeoutException as e: print(traceback.print_exc()) except NoSuchElementException as e: print(traceback.print_exc()) except Exception as e: print(traceback.print_exc()) else: self.driver.find_element_by_id('file').click() os.system('d:\\iDownload\\test.exe')#通過Python提供的os模組的system方法執行產生的test.exe檔案 time.sleep(3) fileSubmitButton = self.driver.find_element_by_id('filesubmit') fileSubmitButton.click() wait.until(EC.title_is('檔案上傳成功')) def tearDown(self): self.driver.quit()if __name__ == '__main__': unittest.main()