Python操作IHTMLDocument2用于自动化测试
2024-09-10 15:48:17
有些软件的界面采用Win32窗口嵌套一个IE控件,用Spy++只能识别出一个Internet Explorer_Server控件。常用的几个API函数无法取到IE控件里面的内容,更无法对里面的控件进行操作,所以这给自动化带来了麻烦。本文将讲述如何使用Python获取IHTMLDocument2接口,用于自动化测试。
获取IHTMLDocument2接口
参考:http://support.microsoft.com/kb/249232
相应的Python实现代码如下:
#!/usr/bin/env python
#coding:utf-8 __author__ = 'CoderZh' import sys # Important for multithreading
sys.coinit_flags = 0 # pythoncom.COINIT_MULTITHREADED import win32com
import win32com.client
import win32gui
import win32con
import pythoncom def getIEServer(hwnd, ieServer):
if win32gui.GetClassName(hwnd) == 'Internet Explorer_Server':
ieServer.append(hwnd) if __name__ == '__main__':
#pythoncom.CoInitializeEx(0) # not use this for multithreading
mainHwnd = win32gui.FindWindow('windowclass', 'windowtitle')
if mainHwnd:
ieServers = []
win32gui.EnumChildWindows(mainHwnd, getIEServer, ieServers)
if len(ieServers) > 0:
ieServer = ieServers[0]
msg = win32gui.RegisterWindowMessage('WM_HTML_GETOBJECT')
ret, result = win32gui.SendMessageTimeout(ieServer, msg, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000)
ob = pythoncom.ObjectFromLresult(result, pythoncom.IID_IDispatch, 0)
doc = win32com.client.dynamic.Dispatch(ob) print doc.url
doc.all['id'].click() #pythoncom.CoUninitialize()
多线程操作
IHTMLDocument2是线程安全的,默认情况下不能在多线程中使用,否则会抛异常。但是在具体使用过程中,又必须使用多线程。解决办法就是上面的代码中的:
# Important for multithreading
sys.coinit_flags = 0 # pythoncom.COINIT_MULTITHREADED
这句必须在开头的时候设定,同时,不要再显示调用pythoncom.CoInitializeEx(0)和 pythoncom.CoUninitialize()。
参考:http://bytes.com/topic/python/answers/26897-multithreaded-com-server-problem
IHTMLDocument2 接口
IHTMLDocument2接口有哪些方法,可以查询http://msdn.microsoft.com/en-us/library/aa752574%28VS.85%29.aspx
基本能够满足自动化测试的需要,可以在此基础上封装出更易使用的自动化UI测试框架。
最新文章
- Ubuntu操作系统下软件的卸载
- C# foreach,等量代换,冒泡排序
- python 循环语句的else语句用法,当循环条件变为假,切不是通过breakbreak终止的时候,就会执行这个else语句。
- [转]理解Linux系统中的load average
- Centos6.7 python2.6升级到python2.7
- Java异常之checked与unchecked
- Android高级音频应用
- 如何在VC中查询中文,及QT5的中文处理
- BZOJ 1501 智慧珠游戏
- JS函数定义与匿名函数的调用
- linux memory release commands内存清理/释放命令
- AE + GDAL实现影像按标准图幅分割(上)
- PTA数组作业一查找整数
- 【转】协同开发中SVN使用规范试用
- 十、JAVA面试简答
- [EXP]Drupal <; 8.5.11 / <; 8.6.10 - RESTful Web Services unserialize() Remote Command Execution (Metasploit)
- MySQL基准测试(二)--方法
- Sprint最后一天
- jQuery 发送 ajax json 请求。。
- java web 程序---刷新页面次数
热门文章
- ASP.NET Zero--9.一个例子(2)商品分类管理-列表
- PHP集成环境自定义设置PHP版本,同时运行多个php版本,700个PHP版本随时切换,一键开启常用模块。
- Altium Designer 快速修改板子形状为Keep-out layer大小
- linux面试题集锦2《转》
- spring mvc @ResponseStatus 注解 注释返回中文乱码的问题
- LeetCode 213. House Robber II
- B+树概念学习
- Python基础知识学习_Day6
- C++ static与单例模式
- [Q]关于无法进入主界面问题解答