• python+selenium上传文件——input标签


    我们要区分出上传按钮的种类,大体上可以分为两种:


    第一种普通上传:将本地文件路径作为一个值,放在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>
  • 相关阅读:
    CF做题记录
    MobaXterm左侧没有文件列表,没有SCP,不显示文件夹问题处理
    使用FastJson转换Object时,空字符串丢失的解决办法【转】
    fastjson处理复杂对象,参数为null问题定位
    python 数据库连接池
    Git找回add 后,未commit的文件(使用reset -hard 命令导致文件清除)
    nginx过滤来自特定IP和user-agent的请求
    Redis实现排行榜(带二位小数点)
    系统不做任何优化,性能提升10%的方法
    二(二)、基于注解形式配置bean
  • 原文地址:https://www.cnblogs.com/xiaobaibailongma/p/12077375.html
Copyright © 2020-2023  润新知