我们要区分出上传按钮的种类,大体上可以分为两种:
第一种普通上传:将本地文件路径作为一个值,放在input标签中,通过form表单将这个值提交给服务器;
第二种插件上传:是通过Flash、JavaScript、Ajax等实现(标签非input)的上传功能;
---------------------------------------------------------------------------------------------------
对于通过input标签实现的上传功能,可以将其看做一个输入框,通过send_keys()指定本地文件路径的方式,
实现文件上传
from selenium import webdriver import time driver = webdriver.Firefox() driver.implicitly_wait(20) driver.get("http://sahitest.com/demo/php/fileUpload.htm") driver.find_element_by_id('file').send_keys('C:\\Users\\del\\Desktop\\文本.txt') time.sleep(10) driver.quit()
---------------------------------------------------------------------------------------------------------
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.implicitly_wait(20)
driver.get("file:///C:/Users/del/Desktop/tanchuang.html")
time.sleep(10)
driver.find_element_by_id('file').send_keys('C:\\Users\\del\\Desktop\\文本.txt')
time.sleep(10)
driver.quit()
===================================================================================
file:///C:/Users/del/Desktop/tanchuang.html的源代码
<html> <body> <form name="form1" action="fileUpload.php" method="post" enctype="multipart/form-data"> <label for="file">File:</label> <input type="file" name="file" id="file" /> <br /> <input type="hidden" name="multi" value="false"/> <input type="submit" name="submit" value="Submit Single" /> </form> <form name="form3" action="fileUpload.php?q=a$&*+^" method="post" enctype="multipart/form-data"> <label for="file">File:</label> <input type="file" name="file" id="file5" /> <br /> <input type="hidden" name="multi" value="false"/> <input type="submit" name="submit" value="Submit Single" /> </form> <form action="fileUpload.php" method="post" enctype="multipart/form-data"> <label for="file2">File[]:</label> <input type="file" name="file[]" id="file2" /> <br /> <label for="file3">File[]:</label> <input type="file" name="file[]" id="file3" /> <br /> <input type="hidden" name="multi" value="true"/> <input type="submit" name="submit" value="Submit Array" /> </form> <script> function setAction(){ document.form2.action = "fileUpload.php"; } </script> <form name="form2" action="" method="post" enctype="multipart/form-data" onsubmit="setAction()"> <label for="file">File:</label> <input type="file" name="file" id="file4" /> <br /> <input type="hidden" name="multi" value="false"/> <input type="submit" name="submit" value="Submit Single" /> </form> <form action="fileUpload.php" method="post" enctype="multipart/form-data"> <label for="files">Files:</label> <input type="file" name="file[]" id="files" multiple/> <br /> <input type="hidden" name="multi" value="true"/> <input type="submit" name="submit" value="Submit Multiple" /> </form> <div id="fileDetails"> <label for="file5">Files:</label> <input type="file" name="file" id="fileWdValidation" multiple/> </div> <!--<div id="filesDetails"> <label for="file2">Files:</label> <input type="file" name="file" id="files1" multiple/> </div> --> <script> function fillFileDetails(){ var files = this._file; var fileDetails = "<br />"; for(var i=0; i<files.length; i++){ fileDetails += "<span class='fileName'>File Name:"+ files[i].name +"</span> <br />"; fileDetails += "<span class='fileSize'>File Size:"+ files[i].size +"</span> <br />"; fileDetails += "<span class='fileType'>File Type:"+ files[i].type +"</span> <br />"; } document.getElementById("fileDetails").insertAdjacentHTML("beforeend", fileDetails); } document.getElementById("fileWdValidation").onchange = fillFileDetails; //document.document.getElementById("files1").onchange = fillFileDetails; </script> </body> </html>