码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-17 11:39:16      阅读:528      评论:0      收藏:0      [点我收藏+]

标签:uila   and   斜杠   wait   int   pip   @class   app   div   

在使用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:

     ……


 



 

 

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

标签:uila   and   斜杠   wait   int   pip   @class   app   div   

原文地址:http://www.cnblogs.com/VseYoung/p/selenium_upload_python.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!