我从博客(http://technapstar.blogspot.fi/2014/07/automation-with-pywinauto.html)了解如何启动Google Chrome并通过pywinauto输入网址,方法如下:
Python 3.6.1rc1 (v3.6.1rc1^0:e0fbe5feee4f9c00f09eb9659c2182183036261a, Mar 4 2017, 20:00:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import pywinauto
>>> pywinauto.__version__
'0.6.2'
>>> from pywinauto.application import Application
>>> app = Application().start("chrome.exe")
Warning (from warnings module):
File "D:\Python\lib\site-packages\pywinauto\application.py", line 982
UserWarning)
UserWarning: 32-bit application should be automated using 32-bit Python (you use 64-bit Python)
>>> app.window_(title='New Tab')
<pywinauto.application.WindowSpecification object at 0x0000000002FFABE0>
>>> app.window_().TypeKeys('{F6}')
<pywinauto.controls.hwndwrapper.DialogWrapper object at 0x0000000004258320>
>>> app.window_().TypeKeys('{ESC}')
<pywinauto.controls.hwndwrapper.DialogWrapper object at 0x0000000004243518>
>>> app.window_().TypeKeys('www.google.com')
<pywinauto.controls.hwndwrapper.DialogWrapper object at 0x0000000004225D68>
>>> app.window_().TypeKeys('{ENTER}')
<pywinauto.controls.hwndwrapper.DialogWrapper object at 0x00000000042436D8>
>>>
然而,当我尝试以同样的方式启动另一个谷歌浏览器窗口时,我遇到以下错误。
>>> app1 = Application().start("chrome.exe")
Warning (from warnings module):
File "D:\Python\lib\site-packages\pywinauto\application.py", line 982
UserWarning)
UserWarning: 32-bit application should be automated using 32-bit Python (you use 64-bit Python)
>>> app1.window_(title='New Tab')
<pywinauto.application.WindowSpecification object at 0x0000000004243470>
>>> app1.window_().TypeKeys('{F6}')
Traceback (most recent call last):
File "D:\Python\lib\site-packages\pywinauto\application.py", line 243, in __resolve_control
criteria)
File "D:\Python\lib\site-packages\pywinauto\timings.py", line 424, in wait_until_passes
raise err
pywinauto.timings.TimeoutError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
app1.window_().TypeKeys('{F6}')
File "D:\Python\lib\site-packages\pywinauto\application.py", line 365, in __getattribute__
ctrls = self.__resolve_control(self.criteria)
File "D:\Python\lib\site-packages\pywinauto\application.py", line 246, in __resolve_control
raise e.original_exception
File "D:\Python\lib\site-packages\pywinauto\timings.py", line 402, in wait_until_passes
func_val = func(*args)
File "D:\Python\lib\site-packages\pywinauto\application.py", line 188, in __get_ctrl
dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))
我谷歌了一下,但找不到答案。有人可以建议pywinauto操作谷歌浏览器的最佳方法是什么?或pywinauto目前不适合操作谷歌浏览器。
[编辑] 仍然没有按照瓦西里的学生示例代码工作,请看这里:
>>> app = Application(backend='uia');
>>> app.start('chrome.exe --force-renderer-accessibility')
<pywinauto.application.Application object at 0x00000000045D80B8>
>>> app.window().type_keys('{F6}')
<pywinauto.controls.uiawrapper.UIAWrapper object at 0x00000000045D8AC8>
到目前为止还可以,但是当我尝试使用另一个Chrome浏览器时,我遇到了以下错误:
>>> app1 = Application(backend='uia');
>>> app1.start('chrome.exe --force-renderer-accessibility')
<pywinauto.application.Application object at 0x0000000003324F28>
>>> app1.window().type_keys('{F6}')
Traceback (most recent call last):
File "D:\Python\lib\site-packages\pywinauto\application.py", line 243, in __resolve_control
criteria)
File "D:\Python\lib\site-packages\pywinauto\timings.py", line 424, in wait_until_passes
raise err
pywinauto.timings.TimeoutError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
app1.window().type_keys('{F6}')
File "D:\Python\lib\site-packages\pywinauto\application.py", line 365, in __getattribute__
ctrls = self.__resolve_control(self.criteria)
File "D:\Python\lib\site-packages\pywinauto\application.py", line 246, in __resolve_control
raise e.original_exception
File "D:\Python\lib\site-packages\pywinauto\timings.py", line 402, in wait_until_passes
func_val = func(*args)
File "D:\Python\lib\site-packages\pywinauto\application.py", line 188, in __get_ctrl
dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))
File "D:\Python\lib\site-packages\pywinauto\findwindows.py", line 87, in find_element
raise ElementNotFoundError(kwargs)
pywinauto.findwindows.ElementNotFoundError: {'backend': 'uia', 'process': 2832}
>>>
答案 0 :(得分:3)
首先,您需要运行Chrome,并为所有网页启用辅助功能(这里有一些Getting Started Guide):
app = Application().start('chrome.exe --force-renderer-accessibility')
使用backend='uia'
(MS UI自动化技术)可以更轻松地自动化第二个Chrome,您需要明确指定它。关于后端和pywinauto基础知识的更多细节可以在test_explorer_google_drive.py中找到(真的建议大家阅读)。它还解释了在何处查找Inspect.exe以浏览UI元素层次结构以及更多内容。只是一个小例子:
app = Application(backend='uia').start('chrome.exe --force-renderer-accessibility')
第三个Chrome可能会在不同的过程中启动新标签。因此,您需要使用title
或title_re
条件连接到此新流程。
app_new_tab = Application(backend='uia').connect(path='chrome.exe', title_re='New Tab')
替代方法是使用Desktop
对象(与进程无关的方式):
NewTab = Desktop(backend='uia').NewTab
NewTab.print_control_identifiers() # prints UI elements subtree
[编辑]我的学生写了一个示例脚本,使用Chrome以隐身模式{{3}}将zip文件从explorer.exe拖到Google云端硬盘。测试英文版。
# encoding: utf-8
from __future__ import print_function
from os import path
from pywinauto import Desktop, Application
chrome_dir = r'"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"'
tab_log_in = u'Meet Google Drive - One place for all your files (Incognito)'
tab_drive = 'My Drive - Google Drive (Incognito)'
# # start google chrome
chrome = Application(backend='uia')
chrome.start(chrome_dir + ' --force-renderer-accessibility --incognito --start-maximized '
'https://accounts.google.com/ServiceLogin?service=wise&passive=1209600&continue=https:'
'//drive.google.com/?urp%3Dhttps://www.google.ru/_/chrome/newtab?espv%253D2%2526ie%253DUT%23&followup='
'https://drive.google.com/?urp%3Dhttps://www.google.ru/_/chrome/newtab?espv%253D2%2526ie%253DUT'
'<mpl=drive&emr=1#identifier')
# wait while a page is loading
chrome[tab_log_in].child_window(title_re='Reload.*', control_type='Button').wait('visible', timeout=10)
ch_window = chrome[tab_drive].child_window(title="Google Chrome", control_type="Custom")
# log in
chrome.window().type_keys('TestPywinauto{ENTER}') # username
chrome[tab_log_in].child_window(title="Google Chrome", control_type="Custom").\
child_window(title="Back", control_type="Image").wait(wait_for='visible', timeout=10)
chrome.window().type_keys('testpywinauto123{ENTER}') # password
ch_window.child_window(title="Getting started PDF", control_type="ListItem").wait(wait_for='visible')
# start explorer in the current path
dir_path = path.join(path.dirname(path.realpath(__file__)), 'UIA_Drive')
explorer = Application().start('explorer.exe ' + dir_path)
Desktop(backend='uia').window(title='UIA_Drive', active_only=True).wait('visible', timeout=10)
dlg = Application(backend='uia').connect(path='explorer.exe', title='UIA_Drive')
# find file
file = dlg.UIA_Drive.ItemsView.wrapper_object().get_item('test.zip')
# drag n drop
destination = chrome.top_window().wrapper_object()
file.press_mouse_input(coords=file.rectangle().mid_point())
file.move_mouse_input(coords=destination.rectangle().mid_point())
chrome.top_window().set_focus()
file.release_mouse_input(coords=destination.rectangle().mid_point())
# wait upload file
ch_window.child_window(title="test.zip Compressed Archive", control_type="ListItem").wait('visible')
print('DONE')
答案 1 :(得分:0)
更简单的开始方法是:
from pywinauto.application import Application
chrome_dir = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
start_args = ' --force-renderer-accessibility --start-maximized https://www.google.com/'
app = Application(backend="uia").start(chrome_dir+start_args)