I am trying to design a function for object recognition that allows for retries. Here is my code snippet below:
def wait_for_object_with_retries(base_object, find_properties, find_values, depth):
attempt = 1
while attempt <= retry:
try:
base_object.Refresh() #base_object.RefreshMappingInfo()
reference_object = None
reference_object = base_object.Find(find_properties, find_values, depth)
if not reference_object.Exists:
raise Exception(“Object not detected.”)
return reference_object
except Exception as e:
Log.Message(str(e))
Delay(1000, “Unable to find object. Waiting for timeout.”)
attempt = attempt + 1
return None
For whatever reason, the Find function’s ability to find an object given parameters is inconsistent. When something is looked for right before it appears, the code attempts to retry two more times. However, even if the new object is demonstrably on screen, the retry has the same result of not finding the object. Any recommendations?
Originally posted by Hassan_Ballan on January 15, 2025
Not sure if any of my suggestions will help.
If you are using NameMapping, TestComplete rely heavily on caching and you will need to use the RefreshMappingInfo for that specific object, it’s parent, or starting from the tree top Browser.
Maybe you can try to split to two functions, one that will loop and check if exist, and to call the other function that refresh and find.
The goal is to find an object again if it is not found immediately. For example, if an object didn’t exist when the function is called, but appears within 3 seconds, the function needs to be able to find the object now that it has appeared.
Have you tried using FindEx or FindChildEx? This will build your delay into the method and simplify your code! It also has a ‘refreshtree’ option you can enable to ensure you can find objects that are dynamically created and cause a problem with the traditional find method.