基础类封装-浏览器文件上载类库封装

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3 # @Time    : 2020/1/7 21:38
 4 # @Author  : Tang Yiwei
 5 # @Email   : 892398433@qq.com
 6 # @File    : uploadfile.py
 7 # @Software: PyCharm
 8 
 9 
10 """
11 win32gui.FindWindow(ipClassName,ipWindowsName)
12 自顶层窗口开始寻找匹配条件的窗口,并返回这个窗口的句柄
13 ipClassName:类名,在Spy++里能够看到;ipWindowsName:窗口名,标题栏能够看到
14 
15 win32gui.FindWindowEx(hwndParent=0,hwndChildAfter=0,ipszClass=None,ipsxWindow=None)
16 搜索类名和窗体名匹配的窗体,并返回这个窗体的句柄,找不到就返回0
17 hwndParent:若不为0,则搜索句柄为hwndParent的子窗体
18 hwndChildAfter:若不为0,则按照z-index的顺序从hwndChildAfter后开始搜索子窗体,否则从第一个子窗体开始搜索
19 ipszClass:字符型,是窗体的类名,可以在Spy++里能够看到
20 ipsxWindow:字符型,是窗口名,标题栏能看到
21 
22 win32gui.SendMessage(hWnd,Msg,wParam,IParam)
23 hWnd:整形,接收消息的窗体句柄
24 Msg:整形,要发送的消息,这些消息都是windows预先设置好的
25 wParam:整形,消息的wParam参数
26 IParam:整形,消息的IParam参数
27 """
28 
29 
30 import win32gui
31 import win32con
32 
33 
34 def upload_file_by_chrome(filepath,syslanguage="en"):
35     """
36     谷歌浏览器上传文件控件
37     :param filepath:上传文件的路径
38     :param syslanguage:操作系统当前的语言环境,默认为en
39     :return:None
40     """
41     if syslanguage == "ch":
42         dialog = win32gui.FindWindow ("#32770", "打开")
43     elif syslanguage == "en":
44         dialog = win32gui.FindWindow("#32770", "open")
45     else:
46         print("不支持的语言!")
47     print(dialog)
48     # 找到窗口
49     comboxex32 = win32gui.FindWindowEx(dialog,0,"ComboBoxEx32",None)
50     print(comboxex32)
51     combox = win32gui.FindWindowEx(comboxex32,0,"ComboBox",None)
52     print(combox)
53     edit = win32gui.FindWindowEx(combox,0,"Edit",None)
54     print(edit)
55     if syslanguage == "ch":
56         button = win32gui.FindWindowEx(dialog, 0, "Button", "打开(&O)")
57     elif syslanguage == "en":
58         button = win32gui.FindWindowEx(dialog, 0, "Button", "&Open")
59     win32gui.SendMessage(edit,win32con.WM_SETTEXT,None,filepath)
60     win32gui.SendMessage(dialog,win32con.WM_COMMAND,1,button) # 点击打开按钮
61 
62 def upload_file_by_firefox(filepath,syslanguage="ch"):
63     """
64     火狐浏览器上传文件控件
65     :param filepath:上传文件的路径
66     :param syslanguage:操作系统当前的语言环境,默认为en
67     :return:None
68     """
69     if syslanguage == "ch":
70         dialog = win32gui.FindWindow ("#32770", "文件上传")
71     elif syslanguage == "en":
72         dialog = win32gui.FindWindow("#32770", "open")
73     else:
74         print("不支持的语言!")
75     print(dialog)
76     # 找到窗口
77     comboxex32 = win32gui.FindWindowEx(dialog,0,"ComboBoxEx32",None)
78     print(comboxex32)
79     combox = win32gui.FindWindowEx(comboxex32,0,"ComboBox",None)
80     print(combox)
81     edit = win32gui.FindWindowEx(combox,0,"Edit",None)
82     print(edit)
83     if syslanguage == "ch":
84         button = win32gui.FindWindowEx(dialog, 0, "Button", "打开(&O)")
85     elif syslanguage == "en":
86         button = win32gui.FindWindowEx(dialog, 0, "Button", "&Open")
87     win32gui.SendMessage(edit,win32con.WM_SETTEXT,None,filepath)
88     win32gui.SendMessage(dialog,win32con.WM_COMMAND,1,button) # 点击打开按钮
89 
90 if __name__ == '__main__':
91     # upload_file_by_chrome(r"C:\Users\Administrator\Desktop\clips\A.mov")
92     upload_file_by_firefox(r"C:\Users\Administrator\Desktop\clips\A.mov")

 

posted @ 2020-04-26 20:27  此去经年归来仍是少年  阅读(235)  评论(0编辑  收藏  举报