無人工幹預地自動上傳附件,人工幹預上傳附件

來源:互聯網
上載者:User

無人工幹預地自動上傳附件,人工幹預上傳附件

<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()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.