Selenium自动化测试--上传文件

转https://blog.csdn.net/qq_32897143/article/details/82014603
https://www.jb51.net/article/92678.htm

安装win32系列
pip install pypiwin32

from selenium import webdriver
import os

# 1.input标签
#对于input标签可以直接send_keys的

# b = webdriver.Chrome()
# b.get("http://www.sahitest.com/demo/php/fileUpload.htm")
# upload_ele = b.find_element_by_id('file')
# upload_ele.send_keys(r'C:\Users\shitou\eclipse-workspace\Web_GUI_Test_Python_Selenium\project\business\test.py') # send_keys
# print(upload_ele.get_attribute('value')


# 2.非input型上传
# 2.1autoIT (参考https://www.cnblogs.com/caoj/p/7815835.html)
#autoit程序
"""
;first make sure the number of arguments passed into the scripts is more than 1
If $CmdLine[0]<2 Then Exit EndIf ;if parmas num <2 ,then break
;$CmdLine[0] ;参数的数量
;$CmdLine[1] ;第一个参数 (脚本名称后面)
;$CmdLine[2] ;第二个参数
;都是从cmd传入参数
 handleUpload($CmdLine[1],$CmdLine[2])

;定义上传函数,有两个参数,第一个是浏览器名字,第二参数是文件路径
 Func handleUpload($browser, $uploadfile)
     Dim $title                          ;定义一个title变量
            ;根据弹窗的title来判断是什么浏览器
            If $browser="ie" Then                          ; 代表IE浏览器
                  $title="选择要加载的文件"
            ElseIf $browser="chrome" Then               ; 代表谷歌浏览器
                 $title="打开"
            ElseIf    $browser="firefox" Then             ; 代表火狐浏览器
                  $title="文件上传"
            EndIf

            if WinWait($title,"",10) Then ;等待弹窗出现,最大等待时间是10秒
                   WinActivate($title)                  ;找到弹出窗口之后,激活当前窗口
                   ControlSetText($title,"","Edit1",$uploadfile)   ;把文件路径放入输入框,此”Edit1“是用FinderTool获取到的
                   ControlClick($title,"","Button1")                ;点击保存或者打开或者上传按钮,此“Button1”使用FinderTool获取到的
            Else
            Return False
            EndIf
 EndFunc
"""

b = webdriver.Chrome()
b.get("http://www.sahitest.com/demo/php/fileUpload.htm")
upload_ele = b.find_element_by_id('file')
upload_ele.click()
#执行程序
os.system("upload.exe %s %s"%("chrome", r"C:\Users\shitou\eclipse-workspace\Web_GUI_Test_Python_Selenium\project\business\test.py"))
dr.quit()


## 2.2 win32gui

import win32con
import win32gui
# # 上传
def open_file_dialog(file_path):
    dialog = win32gui.FindWindow("#32770", u"打开")
    if dialog == 0:
        return False
    assert dialog != 0
    button = win32gui.GetDlgItem(dialog, int("00000001", 16))
    assert button != 0
    edit = win32gui.GetDlgItem(dialog, int("0000047C", 16))
    assert edit != 0
    win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, file_path)
    win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
    return True
  
open_file_dialog(r"C:\Python36\IEDriverServer.exe")
 
 
# # 下载
def open_file_dialog():
    dialog = win32gui.FindWindow("#32770", u"另存为")
    if dialog == 0:
        return False
    assert dialog != 0
    button = win32gui.FindWindowEx(dialog,0,"Button",None)
    assert button != 0
    win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
  
    return True
   
open_file_dialog()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值