本文主要介绍 [自动化测试]文件上传,小编希望对大家在测试领域中测试技术、测试方法、测试思维等有所提高,有助于日常的测试工作。
对于通过input标签实现的上传功能,可以将其看作是一个输入框,即通过send_keys()指定本地文件路径的方式实现文件上传。
创建upfile.html文件,代码如下:
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>upload_file</title> <link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <div class="row-fluid"> <div class="span6 well"> <h3>upload_file</h3> <input type="file" name="file" /> </div> </div> </body> <script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></scrip> </html>
通过浏览器打开upfile.html文件,功能如下图。
您现在正在阅读的是由小熊分享邦为您整理的 [自动化测试]文件上传。
接下来通过send_keys()方法来实现文件上传。
from selenium import webdriver import os driver = webdriver.Firefox() file_path = 'file:///' + os.path.abspath('upfile.html') driver.get(file_path) # 定位上传按钮,添加本地文件 driver.find_element_by_name("file").send_keys('D://upload_file.txt') driver.quit()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小熊分享邦(www.xxfxb.com),希望您在日常工作中得到提升,谢谢。