zoukankan      html  css  js  c++  java
  • selenium+Python(文件上传)

           文件上传操作也比较常见功能之一,上传功能没有用到新有方法或函数,
    上传过程一般要打开一个本地窗口,从窗口选择本地文件添加。所以,一般会卡在如何操作本地窗口添加上传文件
    只要定位上传按钮,通send_keys 添加本地文件路径就可以了。绝对路径和相对路径都可以,关键是上传的文
    件存在。下面通地例子演示。

    1、send_keys()实现上传

     对于通过input标签实现的上传功能,可以将其看作是一个输入框,即通过send_keys()指定本地文件路径的方式实现文件上传

    前期需要准备待的上传文件

    为了便于演示,自己手动洗了简单的html页面:upfile.html

    <html>
    <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>upload_file</title>
    <script type="text/javascript"
    async=""src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
    "></script>
    <link
    href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstra
    p-combined.min.css" rel="stylesheet" />
    <script type="text/javascript">
    </script>
    </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://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.
    min.js"></script>
    </html>
    

     上传的脚本文件test_case.py

    #coding=utf-8
    from selenium import webdriver
    import os,time
    driver = webdriver.Firefox()
    #脚本要与 upload_file.html 同一目录
    file_path = 'file:///' + os.path.abspath('upload_file.html')
    driver.get(file_path)
    #定位上传按钮,添加本地文件
    driver.find_element_by_name("file").send_keys('C:\Users\Anne\Desktop\SeleniumPython_Test\test_upandload_fileupload_file.txt')
    print ("file has up")
    time.sleep(2)
    driver.quit()  

    注意:

    有些上传按钮在frame里面,就需要先点位到frame上,然后再定位到上传按钮

    有时 iframe 的 id 是动态的,且没有 name 属性,其它属性也不是很明显,可以通过标签定位所有的 iframe 标签,然后取对应的第几个就可以了

  • 相关阅读:
    BZOJ 1143 [CTSC2008]祭祀river
    BZOJ 3997 [TJOI2015]组合数学
    BZOJ 3996 [TJOI2015]线性代数
    BZOJ 4553 [Tjoi2016&Heoi2016]序列
    微信开发之密文模式 mcrypt_module_open 走不过
    JS JSON & ARRAY 遍历
    linux ftp服务器配置(Ubuntu)
    thinkphp 吐槽篇
    游戏--疯狂猜字随机混乱正确答案逻辑
    PHP 批量去除BOM头;此文转载;
  • 原文地址:https://www.cnblogs.com/101718qiong/p/7458393.html
Copyright © 2011-2022 走看看