无人工干预地自动上传附件

2018-01-12 21:17:20来源:cnblogs.com作者:sleeping_cat人点击

分享
<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:   [email protected] : [email protected] """ #无人工干预地自动上传附件#使用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:   [email protected] : [email protected] """ #无人工干预地自动上传附件#模拟键盘操作,实现上传文件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:   [email protected] : [email protected] """ #无人工干预地自动上传附件#使用第三方工具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()

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台