Forum Discussion

rangaperera's avatar
Occasional Contributor
8 years ago

Waiting until the window is activated

I have following code that sets off a timer periodically and click "OK" a button. At the moment, I'm seeing two issues.  


1) mouse moves on to the "OK" button (since there is no mouse move commands, it must be executing the mouse click), but the "OK" button does not get clicked. (Wait for the button object can see the "OK" button).


2) After missed button click, I see message saying "Waiting until the window is activated" and id doesnt go away. (Without the timer, "OK" button click works fine).  It looks as if control doesnt get returned to the main for loop after handleRestartDeviceCautionForm() gets executed. 


During the time handleRestartDeviceCautionForm() executes, I need rest of the code to pause, because this "Restart Device Form" blocks all the functionality in the application. This is being run on Win7 with Python 3.4.3.  Can anyone show me how to over come this issue? 


Below is the code:

#Main func
cancel_future_calls = call_repeatedly(60, handleRestartDeviceCautionForm)
for i in range(120):
cancel_future_calls() # stop future calls

#Timer code
from threading import Timer
import threading
from threading import Event, Thread
def call_repeatedly(interval, func, *args):
    stopped = Event()
    def loop():
         while not stopped.wait(interval): # the first call is in `interval` secs
   return stopped.set

#Code that handles ok button
def handleRestartDeviceCautionForm():
      Tgence = ConfigTool()
      if Tgence.getzremindBtn().Exists:



3 Replies

  • tristaanogre's avatar
    Esteemed Contributor

    OK, I'm not at all experienced in Python so some of what I'm seeing may just be me.  However, look at the following line of code:


    if Tgence.getzremindBtn().Exists

    I'm not sure what "getzremindBtn()" is... it appears to be a call to an object. If this is the case, and there's not some other processing going on, you are checking for the "exists" property on an object that, potentially, does not exist yet. This could cause some odd errors and problems.  Check out the following topic and see if you can add one of those WaitNNN methods to your code in order to better handle this code.

    • rangaperera's avatar
      Occasional Contributor

      Thank you very much for responding. 


      In this case, it does find the object, every time. Then it seems like mouse click even gets executed to a certain degree. What I mean by that is, since the curser move into the correct position for the mouse click, I assume mouse click gets executed. However, "OK" button never gets pressed. 

      • tristaanogre's avatar
        Esteemed Contributor

        Another possibility could be that there is a "flicker" in the object. It is found, it exists, and the click method appears to work, but the object may change state somewhere in the process where the click may execute but it may not actually be valid at that point. This is something I've rarely seen in my experience but it is, technically, possible.

        Just as a diagnostic bit, have you tried putting a Delay call in the code before your call to click? Add maybe a second or two delay before the click to give the application a chance to "stabilize" before the click is excuted. What this might indicate, if it works, is that you need to do some additional checks other than "exists" to make sure that you can actually click the button. You might need to use a WaitProperty call on the button to make sure it's enabled.


        Just a few things to try.