分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

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

发布时间:2023-09-06 01:36责任编辑:苏小强关键词:暂无标签
<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()

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

原文地址:https://www.cnblogs.com/sleeping-cat/p/8277568.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved