selenium实现文件上传方法汇总(AutoIt、win32GUI、sengkeys)---基于python

在使用selenium进行UI自动化测试时,经常会遇到一个关于本地文件上传的问题,解决此问题一般分两种情况:

1. 元素标签为input

2.非input型上传

下面我们分别对着两种情况进行实例分析

(一)元素标签为input

此种情况处理比较简单,标签为input的上传,可以直接通过send_keys("本地文件路径")实现

举例:(以百度网盘为例)



 1 # coding:utf-8
 2 from selenium import webdriver
 3 from time import sleep
 4 #声明配置文件路径,在路径前加r标识后面跟的路径原意输出,如果不加r需要对路径中的斜杠进行转义
 5 profile_directory=r"C:\Users\55348\AppData\Roaming\Mozilla\Firefox\Profiles\tqc0968l.default"
 6 #加载配置文件
 7 profile=webdriver.FirefoxProfile(profile_directory)
 8 #启动浏览器配置
 9 driver=webdriver.Firefox(profile)
10 #打开url
11 driver.get("http://pan.baidu.com")
12 #隐式等待
13 driver.implicitly_wait(10)
14 #点击上传,由于上传按钮是input属性的,所以可以直接通过send_keys
15 driver.find_element_by_id("h5Input0").send_keys(r"C:\Users\55348\Desktop\03913f358d9be352bd125ae7087dd0d6.apk")
16 sleep(10)
17 #判断是否上传成功
18 new=driver.find_elements_by_xpath("//*[@title=‘03913f358d9be352bd125ae7087dd0d6.apk‘ and @class=‘xj9QOe‘]")
19 if len(new)==1:
20     print "upload apk ok"
21 else:
22     print "upload apk failed"
23 driver.quit()

 

(二)非input情况


此种情况处理比较复杂 ,有三种处理方式:①使用 SendKeys第三方库     ②使用AutoIt第三方工具  ③使用win32 GUI工具

  1.1 使用SendKeys第三方库

  首先进行SendKeys第三方库

  pip install SendKeys

  如果出现如下提示:就多试几次。

  如果出现如下提示:,这种情况,可以根据提示Get it from http://aka.ms/vcpython27

即登录此地址下载C++,下载完成后傻瓜式安装即可。

  其次,在第三方库安装完成后,即可进行上传的动作,具体如下:



 1 # coding:utf-8
 2 from selenium import webdriver
 3 from time import sleep
 4 import SendKeys
 5 from selenium.webdriver.common.keys import Keys
 6 #声明配置文件路径,在路径前加r标识后面跟的路径原意输出,如果不加r需要对路径中的斜杠进行转义
 7 profile_directory=r"C:\Users\55348\AppData\Roaming\Mozilla\Firefox\Profiles\tqc0968l.default"
 8 #加载配置文件
 9 profile=webdriver.FirefoxProfile(profile_directory)
10 #启动浏览器配置
11 driver=webdriver.Firefox(profile)
12 #打开url
13 driver.get("http://pan.baidu.com")
14 #隐式等待
15 driver.implicitly_wait(10)
16 #仍以百度网盘为例,本次通过点击上传按钮,进行上传文件选择
17 driver.find_element_by_id("h5Input0").click()
18 SendKeys.SendKeys(r"C:\Users\55348\Desktop\03913f358d9be352bd125ae7087dd0d6.apk")
19 sleep(2)
20 SendKeys.SendKeys("{ENTER}")   #enter键
21 sleep(2)
22 SendKeys.SendKeys("{ENTER}")
23 sleep(5)
24 #判断是否上传成功
25 new=driver.find_elements_by_xpath("//*[@title=‘03913f358d9be352bd125ae7087dd0d6.apk‘ and @class=‘xj9QOe‘]")
26 if len(new)==1:
27     print "upload apk ok"
28 else:
29     print "upload apk failed"
30 driver.quit()

  1.2 使用AutoIt工具

  下面文章摘录于http://blog.csdn.net/huilan_same/article/details/52208363

  1.3 使用Win32 GUI

  下面文章摘录于http://blog.csdn.net/huilan_same/article/details/52439546

备注:

  多文件的上传,可以将文件路径加入到list中,然后通过for循环读取并上传

  upload_directory=[]

  upload_directory.append[r"c:\1.txt"]

  upload_directory.append[r"c:\2.txt"]

  upload_directory.append[r"c:\3.txt"]

  for file in upload_directory:

     ……




 

 

时间: 2024-05-24 02:51:57

selenium实现文件上传方法汇总(AutoIt、win32GUI、sengkeys)---基于python的相关文章

selenium之 文件上传所有方法整理总结

本文转载"灰蓝"的原创博客.http://blog.csdn.net/huilan_same/article/details/52439546 文件上传是所有UI自动化测试都要面对的一个头疼问题,今天博主在这里给大家分享下自己处理文件上传的经验,希望能够帮助到广大被文件上传坑住的seleniumer. 首先,我们要区分出上传按钮的种类,大体上可以分为两种,一种是input框,另外一种就比较复杂,通过js.flash等实现,标签非input 我们分别对这两种进行分析: 1.input标签

spring mvc文件上传方法

spring mvc上传功能很强大. spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype="multipart/form-data" 这个是上传文件必须的2.applicationContext.xml中 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolv

selenium学习——文件上传

1.上传 学习链接: https://www.cnblogs.com/yoyoketang/p/6445270.html input标签可以借助send_keys()操作来实现文件上传 <<input type="file" name="file" style="position: absolute; right: 0px; top: 0px; font-family: Arial; font-size: 118px; margin: 0px;

Selenium的文件上传JAVA脚本

在写文件上传脚本的时候,遇到了很多问题,包括元素定位,以及上传操作,现在总结下来以下几点: 1. 上传的控件定位要准确,必要时要进行等待 WebElement adFileUpload = driver.findElement(By.xpath("//input[@type='file']")); 2. 上传操作 String filePath ="D:\\taxonomy\\DB\\HaierTaxonomy.xlsx"; adFileUpload.sendKey

php单文件上传方法 转

** * 单文件上传 * @method upload_file * @param  array       $fileInfo   上传文件的信息,是一个数组 * @param  string      $uploadPath 文件上传默认路径 * @param  boolean     $imageFlag  是否检测真实图片 * @param  array       $allowExt   允许上传的文件类型 * @return mixed                  成功返回文件

selenium之文件上传

在这里和大家分享二种selenium中上传文件的方法. 方法一:使用input,type=file来实现 具体代码如下: e=driver.find_element_by_name("filename")   #定位上传文件的元素 time.sleep(2) e.send_keys(r'D:\demo.xlsx')  #  需要上传的文件路径 driver.quit() 方法二:因有些上传文件的不是采用form表单形式,点击上传文件按钮后需要和Windows弹框进行交互. 原文地址:h

Python Selenium 文件上传(一)

昨天写了Web 文件下载的ui自动化,下载之后,今天就要写web 文件上传的功能了. 当然从折腾了俩小时才上传成功.下面写一下自己操作的步骤 首先网上说的有很多方法 如 input 标签的最好做了,直接定位到元素,然后再sendKeys("value")即可 <input id="file_name" class="text-1 w255" type="text" readonly="" value=

Python WebDriver 文件上传

昨天写了Web 文件下载的ui自动化,下载之后,今天就要写web 文件上传的功能了. 当然从折腾了俩小时才上传成功.下面写一下自己操作的步骤 首先网上说的有很多方法 如 input 标签的最好做了,直接定位到元素,然后再sendKeys("value")即可 <input id="file_name" class="text-1 w255" type="text" readonly="" value=

Selenium怎么优化AutoIT文件上传?

Selenium优化文件上传 Selenium的文件上传是有缺陷的,只能支持Input标签上传.这是Selenium的硬伤,但是我们想到了用AutoIT的方式解决了这个问题.但是autoIT使用起来太过于麻烦.在这儿我们之前是先用autoIT识别元素,再编写脚本,再编译成exe执行文件,再用java代码去调用exe执行文件.这样的流程是不是显得很复杂呢?而且还要编写autoIT的脚本.所以作者想能不能直接编写Java脚本代替原来的autoIT脚本呢?后来作者找到了autoIT的集成包.只要将Au