python 调用 java access bridge

python 调用 java access bridge

在github上找到的python应用jab实例,基本上可以使用了,能够得到applet上控件的属性值,也能给文本框赋值,点击按钮等操作都成功了,但又出现了一个新问题,点击按钮之后,网页中的applet需要响应1秒钟左右才会出现新的内容,我想做的是把一些数据逐一输入到文本框中,然后点击按钮,取出applet刷新出来的新东西,重复这个动作。但问题是这个循环动作只能进行一两次,python就自动退出了,更多的时候是只进行一次,最下面那句print('click' finished)还没运行就退出了,这是啥原因?

补充一点,发现一个新问题,当代码正在运行的时候,如果我手动操控一下那个applet,比如点一下按钮。python代码就会自动终止并退出了。当模拟点击button事件之后,那个applet会做出响应,在一个文本框中出现一些数据,这时候phthon代码非常容易自动终止,这是啥原因?

发现了问题所在,在python 代码运行过程中,只要某一个变量和applet发生了关联,即通过jab获取了applet上的某个控件,那么此时如果这个applet的状态发生了变动,那么python的kernel就很容易发生崩溃,比如click事件发出后,applet会做出响应,在文本窗中出现一些信息,此时python kernel就崩溃了,具体的根源是啥不清楚,也不知道咋解决

import ctypes

import ctypes.wintypes

import threading

user32 = ctypes.windll.user32

import time

#=================================================================

def initialize():

bridgeDll = cdll.LoadLibrary(r'C:\XXX\windowsaccessbridge-64.dll')

# Accept wm_copydata and any wm_user messages from other processes even if running with higher privileges

if not windll.user32.ChangeWindowMessageFilter(winUser.WM_COPYDATA, 1):

raise WinError()

for msg in range(winUser.WM_USER + 1, 0xffff):

if not windll.user32.ChangeWindowMessageFilter(msg, 1):

raise WinError()

bridgeDll.Windows_run()

#=================================================================

class MyThread(threading.Thread):

def run(self):

msg = ctypes.wintypes.MSG()

initialize()

while user32.GetMessageW(ctypes.byref(msg), None, 0, 0):

user32.TranslateMessage(msg)

user32.DispatchMessageW(msg)

print('End of GetMessageW')

#=================================================================

#call dll initialize in the thread

t = MyThread()

t.start()

time.sleep(1)

#=================================================================

hwnd = 0x000D0E7A

print('java window is ', isJavaWindow(hwnd)) # java window is 1

#=================================================================

#To create instance

class JABContext(object):

def __init__(self,hwnd=None,vmID=None,accContext=None):

if hwnd and not vmID:

vmID=c_long()

accContext=JOBJECT64()

bridgeDll.getAccessibleContextFromHWND(hwnd,byref(vmID),byref(accContext))

#Record this vm ID and window handle for later use with other objects

vmID=vmID.value

vmIDsToWindowHandles[vmID]=hwnd

elif vmID and not hwnd:

hwnd = getWindowHandleFromAccContext(vmID,accContext)

self.hwnd=hwnd

self.vmID=vmID

self.accContext=accContext

def getAccessibleContextInfo(self):

info=AccessibleContextInfo()

bridgeDll.getAccessibleContextInfo(self.vmID,self.accContext,byref(info))

return info

def getAccessibleTextItems(self,index):

textItemsInfo=AccessibleTextItemsInfo()

bridgeDll.getAccessibleTextItems(self.vmID,self.accContext,byref(textItemsInfo),index)

return textItemsInfo

def getAccessibleChildFromContext(self,index):

accContext=bridgeDll.getAccessibleChildFromContext(self.vmID,self.accContext,index)

if accContext:

return self.__class__(self.hwnd,self.vmID,accContext)

else:

return None

#To input words into text controls

def setTextContents(self, word):

bridgeDll.setTextContents(self.vmID,self.accContext,word)

#To get actions structure

def getAccessibleActions(self):

info = AccessibleActions()

bridgeDll.getAccessibleActions(self.vmID,self.accContext,byref(info))

return info

#To do actions click

def doAccessibleActions(self, AccessibleActionsToDo):

info = AccessibleActionsToDo

failure = jint()

bridgeDll.doAccessibleActions(self.vmID,self.accContext,byref(info),byref(failure))

return failure

Jab = JABContext(hwnd = hwnd,vmID = None, accContext = None)

Textbox = Jab.getAccessibleChildFromContext(index = 0)

button = Jab.getAccessibleChildFromContext(index = 1)

#=================================================================

list_test = [1,2,3,4,5]

for num in list_test:

#set text

Textbox.setTextContents(num)

TempNum = Textbox.getAccessibleTextItems(index = 0).word

if TempNum != num:

print('input error')

print('input finished')

#click on button

getAccessibleActions = button.getAccessibleActions()

aa = button.doAccessibleActions(getAccessibleActions)

if aa == 0:

print('click failure')

print('click' finished)

以上是 python 调用 java access bridge 的全部内容, 来源链接: utcz.com/p/937951.html

回到顶部