[pywin32-bugs] problem in clicking a button
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: mahesh <ma...@pr...> - 2004-11-26 11:40:23
|
Dear all, =20 I was trying to automate the process of GUI mode installation of a = product. =20 Here is a sample piece of code that i was using to click a button = (It works on most applications ) : -------------------------------------------------------------------------= ------------------------------------------------------------------- =20 ''' Get the next button on the window handle and Click "Next" ''' def clickNext(xWindow,pageno,button=3D"Next"): print "\t\t Clicking button !! ",button btnEnter=3D[] try: btnEnter=3D findControl(xWindow, wantedClass=3D"Button", = wantedText=3Dbutton) if btnEnter: print "\t Able to get the button = ",win32gui.GetWindowText(btnEnter[0]) except: if btnEnter: print "Unable to get the button = ",win32gui.GetWindowText(btnEnter[0]) print " $$$ EXCEPTION CLICKING THE BUTTON :> ",button," id=3D = ",btnEnter #return 0 return 0 clickButton(btnEnter[0]) print "Operation Button Press on = ",win32gui.GetWindowText(btnEnter[0])," button on page ",pageno," on = window ",xWindow return 1 ''' Simulates a single mouse click on a button ''' def clickButton(hwnd): #print "Clicking ",win32gui.GetWindowText(hwnd) _sendNotifyMessage(hwnd, win32con.BN_CLICKED) #print "Clicked ",win32gui.GetWindowText(hwnd) def _sendNotifyMessage(hwnd, nofifyMessage): '''Send a notify message to a control.''' hbutton=3Dhwnd #print "WindowLong =3D = ",win32api.GetWindowLong(hwnd,win32con.GWL_ID) print "Clicking ",win32gui.GetWindowText(hwnd) = #win32gui.SendMessage(win32gui.GetParent(hwnd),win32con.WM_COMMAND,_build= WinLong(nofifyMessage,win32api.GetWindowLong(hwnd,win32con.GWL_ID)),hwnd)= win32api.PostMessage(hbutton, win32con.WM_LBUTTONDOWN, 0, 0) win32api.PostMessage(hbutton, win32con.WM_LBUTTONUP, 0, 0) print "Clicked ",win32gui.GetWindowText(hwnd) ''' Return a list of of controls of the given text and class ''' def = findControl(topHwnd,wantedText=3DNone,wantedClass=3DNone,selectionFunctio= n=3DNone): controls =3D findControls(topHwnd, wantedText=3DwantedText, wantedClass=3DwantedClass, selectionFunction=3DselectionFunction) if controls: return controls else: raise WinGuiAutoError("No control found for topHwnd=3D" + repr(topHwnd) + ", wantedText=3D" + repr(wantedText) + ", wantedClass=3D" + repr(wantedClass) + ", selectionFunction=3D" + repr(selectionFunction)) def = findControls(topHwnd,wantedText=3DNone,wantedClass=3DNone,selectionFuncti= on=3DNone): def searchChildWindows(currentHwnd): results =3D [] childWindows =3D [] try: win32gui.EnumChildWindows(currentHwnd, _windowEnumerationHandler, childWindows) except win32gui.error: # This seems to mean that the control *cannot* have child = windows, # i.e. not a container. return for childHwnd, windowText, windowClass in childWindows: descendentMatchingHwnds =3D searchChildWindows(childHwnd) if len(windowText)>0: pass #print "childwindows : >",windowText, ">",len(windowText) if descendentMatchingHwnds: results +=3D descendentMatchingHwnds if wantedText and \ not _normaliseText(wantedText) in = _normaliseText(windowText): continue if wantedClass and \ not windowClass =3D=3D wantedClass: continue if selectionFunction and \ not selectionFunction(childHwnd): continue results.append(childHwnd) return results return searchChildWindows(topHwnd) ''' Simulates a single mouse click on a button ''' def clickButton(hwnd): #print "Clicking ",win32gui.GetWindowText(hwnd) _sendNotifyMessage(hwnd, win32con.BN_CLICKED) #print "Clicked ",win32gui.GetWindowText(hwnd) def _sendNotifyMessage(hwnd, nofifyMessage): '''Send a notify message to a control.''' hbutton=3Dhwnd #print "Clicking >: ",hwnd, " || Parent is > = ",win32gui.GetParent(hwnd) #print "WindowLong =3D = ",win32api.GetWindowLong(hwnd,win32con.GWL_ID) print "Clicking ",win32gui.GetWindowText(hwnd) = #win32gui.SendMessage(win32gui.GetParent(hwnd),win32con.WM_COMMAND,_build= WinLong(nofifyMessage,win32api.GetWindowLong(hwnd,win32con.GWL_ID)),hwnd)= win32api.PostMessage(hbutton, win32con.WM_LBUTTONDOWN, 0, 0) win32api.PostMessage(hbutton, win32con.WM_LBUTTONUP, 0, 0) print "Clicked ",win32gui.GetWindowText(hwnd) def _buildWinLong(high, low): # return ((high << 16) | low) return int(struct.unpack('>L',struct.pack('>2H',high,low)) [0]) -------------------------------------------------------------------------= -------------------------------------------------------------------------= ------------------------- Interestingly ,the installshield is coded in Java. So the problem i = am facing is as follows : Problem : I am unable to click the "Next" button using the above = code for automation . Description of the problem : When we start the installer GUI comes up . Keep Pressing next = (manually) screen1 -> screen2 -> screen 3 -> ...screen n ->......screen = z(Finish screen). Once we reach the nth screen and run the script to view all = childwindows of the current installer window .I am able to get the = handles of all the components that were residing in all the screens from = 1 to n. I am also able to edit the text in the textfield component and = click to select the radio buttons. But most importantly i am unable to click the button "Next" through = the above code. In other words , Manual clicks on 'Next" works (Takes me = from screen n to screen [n+1] ) but not through the above code . So, is this a problem with respect to Java only (Java's Internal = Event handling Architecture) ? or Do u think any code level changes can be made to make the "Click" = work ? Thanks in advance, Mahesh |