0
点赞
收藏
分享

微信扫一扫

python UIA操作浏览器

安装uiautomation

pip install uiautomation

操作FireFox实例

 

1 #!python3
2 # -*- coding: utf-8 -*-
3 # Firefox version must <= 56 or >= 60
4 import os
5 import sys
6 import time
7
8 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # not required after 'pip install uiautomation'
9 import uiautomation as automation
10
11
12 def main():
13 firefoxWindow = automation.WindowControl(searchDepth = 1, ClassName = 'MozillaWindowClass')
14 if not firefoxWindow.Exists(0):
15 automation.Logger.WriteLine('please run Firefox first', automation.ConsoleColor.Yellow)
16 return
17 firefoxWindow.ShowWindow(automation.ShowWindow.Maximize)
18 firefoxWindow.SetActive()
19 time.sleep(1)
20 tab = firefoxWindow.TabControl()
21 newTabButton = tab.ButtonControl(searchDepth= 1)
22 newTabButton.Click()
23 edit = firefoxWindow.EditControl()
24 # edit.Click()
25 edit.SendKeys('http://www.bing.com/?rb=0&setmkt=en-us&setlang=en-us{Enter}')
26 time.sleep(2)
27 searchEdit = automation.FindControl(firefoxWindow,
28 lambda c, d: (isinstance(c, automation.EditControl) or isinstance(c, automation.ComboBoxControl)) and c.Name == 'Enter your search term'
29 )
30 # searchEdit.Click()
31 searchEdit.SendKeys('Python-UIAutomation-for-Windows site:github.com{Enter}', 0.05)
32 link = firefoxWindow.HyperlinkControl(SubName = 'yinkaisheng/Python-UIAutomation-for-Windows')
33 automation.Win32API.PressKey(automation.Keys.VK_CONTROL)
34 link.Click() #press control to open the page in a new tab
35 automation.Win32API.ReleaseKey(automation.Keys.VK_CONTROL)
36 newTab = tab.TabItemControl(SubName = 'yinkaisheng/Python-UIAutomation-for-Windows')
37 newTab.Click()
38 starButton = firefoxWindow.ButtonControl(Name = 'Star this repository')
39 if starButton.Exists(5):
40 automation.GetConsoleWindow().SetActive()
41 automation.Logger.WriteLine('Star Python-UIAutomation-for-Windows after 2 seconds', automation.ConsoleColor.Yellow)
42 time.sleep(2)
43 firefoxWindow.SetActive()
44 time.sleep(1)
45 starButton.Click()
46 time.sleep(2)
47 else:
48 unstarButton = firefoxWindow.ButtonControl(Name = 'Unstar this repository')
49 if unstarButton.Exists(0, 0):
50 automation.GetConsoleWindow().SetActive()
51 automation.Logger.WriteLine('Thank you. You have starred.', automation.ConsoleColor.Yellow)
52
53 if __name__ == '__main__':
54 main()
55 automation.GetConsoleWindow().SetActive()
56 input('press Enter to exit')

 

操作Chrome实例

1 __author__ = 'robin'
2 import subprocess
3 import uiautomation as automation
4 import time
5
6 def main():
7 subprocess.Popen('test.exe') #启动浏览器
8 time.sleep(1)
9 redWindow = automation.WindowControl(searchDepth = 1, ClassName = 'Chrome_WidgetWin_1') #取浏览器handle
10 if not redWindow.Exists(0):
11 automation.Logger.WriteLine('未找到测试浏览器,请重试!',automation.ConsoleColor.Yellow)
12 return
13 redWindow.ShowWindow(automation.ShowWindow.Maximize) #最大化浏览器
14 redWindow.SetActive()
15 time.sleep(1)
16
17 menuOperation(redWindow)
18
19
20 def menuOperation(redWindow):
21
22 '''
23 操作浏览器菜单, menuSingleItems包含的项,只操作没有下级菜单的选项
24 Operation test browser mainMenu, only include Level 1 menus
25 '''
26 menuSingleItems = [u'打开新的标签页(T)',u'下载内容(D) Ctrl+J',u'查找(F)... Ctrl+F',u'设置(S)',u'关于 测试企业浏览器(C)',u'打印(P)... Ctrl+P',u'打开新的窗口(N) Ctrl+N',u'退出(X) Ctrl+Shift+Q']
27
28 for chindMenu in menuSingleItems:
29 time.sleep(1)
30
31 if chindMenu == u'打印(P)... Ctrl+P':
32 redWindow.MenuItemControl(searchDepth=5,searchWaitTime=10,foundIndex = 2,name=u'测试企业浏览器').Click()
33 time.sleep(1)
34 searchEdit = automation.FindControl(redWindow,lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance(c, automation.MenuControl)) and c.Name == chindMenu)
35 searchEdit.Click()
36 time.sleep(3)
37 redWindow.SendKeys('{Esc}')
38
39 elif chindMenu == u'打开新的窗口(N) Ctrl+N':
40 redWindow.MenuItemControl(searchDepth=5,searchWaitTime=10,foundIndex = 2,name=u'测试企业浏览器').Click()
41 time.sleep(1)
42 searchEdit = automation.FindControl(redWindow,lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance(c, automation.MenuControl)) and c.Name == chindMenu)
43 searchEdit.Click()
44 time.sleep(2)
45 redWindow.SendKeys('{Alt}{F4}')
46
47 elif chindMenu == u'退出(X) Ctrl+Shift+Q':
48 reredWindow = automation.WindowControl(searchDepth = 1, ClassName = 'Chrome_WidgetWin_1')
49 reredWindow.MenuItemControl(searchDepth=5,searchWaitTime=10,foundIndex = 2,name=u'测试企业浏览器').Click()
50 time.sleep(1)
51 searchEdit = automation.FindControl(reredWindow,lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance(c, automation.MenuControl)) and c.Name == chindMenu)
52 searchEdit.Click()
53 time.sleep(2)
54
55
56 else :
57 redWindow.MenuItemControl(searchDepth=5,searchWaitTime=10,foundIndex = 2,name=u'测试企业浏览器').Click()
58 time.sleep(1)
59 searchEdit = automation.FindControl(redWindow,lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance(c, automation.MenuControl)) and c.Name == chindMenu)
60 searchEdit.Click()
61 print("一级菜单循环数据 " + chindMenu)
62 else:
63 print("没有循环数据!")
64 print("一级菜单完成循环!")
65
66 menuMulItems = [u'历史记录(H)',u'书签(B)',u'更多工具(L)',u'退出(X) Ctrl+Shift+Q']
67
68 for chindMenu in menuMulItems:
69 time.sleep(1)
70
71 if chindMenu == u'历史记录(H)':
72 searchEdit = automation.FindControl(redWindow,lambda c, d: (isinstance(c, automation.EditControl) and c.Name == u'地址和搜索栏'))
73 print(searchEdit)
74 searchEdit.SendKeys('www.test.cn{Enter}',0.05)
75 time.sleep(2)
76 redWindow.MenuItemControl(searchDepth=5,searchWaitTime=10,foundIndex = 2,name=u'测试企业浏览器').Click()
77 time.sleep(1)
78 searchEdit = automation.FindControl(redWindow,lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance(c, automation.MenuControl)) and c.Name == chindMenu)
79 searchEdit.Click()
80 time.sleep(2)
81 searchEdit = automation.FindControl(redWindow,lambda c, d: (isinstance(c, automation.MenuItemControl) or isinstance(c, automation.MenuControl)) and c.Name == u'历史记录(H) Ctrl+H')
82 searchEdit.Click()
83 time.sleep(3)
84 elif chindMenu == u'书签(B)':
85 print("多级菜单循环数据 " + chindMenu)
86 else:
87 print("没有循环数据!")
88 print("多级菜单完成循环!")
89
90
91
92 if __name__ == '__main__':
93 main()
94 automation.GetConsoleWindow().SetActive()
95 input('press Enter to exit')
96
97
98 edit = redWindow.EditControl()
99 edit.SetValue('Hello')
100 edit.SendKeys('{Ctrl}{End}{Enter}World'

 

WindowControl可以定位的方法

def WindowControl(self, element = 0, searchDepth = 0xFFFFFFFF, searchWaitTime = SEARCH_INTERVAL, foundIndex = 1, **searchPorpertyDict):
return WindowControl(element=element, searchDepth=searchDepth, searchWaitTime=searchWaitTime, foundIndex=foundIndex, searchFromControl = self,**searchPorpertyDict)

"""
element: integer
searchFromControl: Control, if is None, search from root control
searchDepth: integer, max search depth from searchFromControl
foundIndex: integer, value must be greater or equal to 1
searchWaitTime: float, wait searchWaitTime before every search
searchPorpertyDict: a dict that defines how to search, only the following keys are valid
ControlType: integer in classClassName: str or unicode
AutomationId: str or unicode
Name: str or unicode
SubName: str or unicode
RegexName: str or unicode, supports regex
Depth: integer, exact depth from searchFromControl, if set, searchDepth will be set to Depth too
"""

 



举报

相关推荐

0 条评论