Forum Discussion

melanieKno's avatar
Occasional Contributor
2 years ago

try-except around FindElement fails (Python)

Web test

I have a form which provides different input fields based on the selected option. Not every option is available every time, but the rest of the code is more or less the same, that's why I don't want to have the same function ten times with only very small differences. So I tried:

All three try-except are executed, everytime it runs into the except-part, but only for the first one with the partname TestComplete stops because of an error? I don't understand this behaviour. I would expect to only have the log messages and executing the rest of the code. How I can avoid the interruption of the test? Why is the last log message logged, but the code after that is not executed? I would appreciate if someone could bring a bit of light into this.

4 Replies

  • Kitt's avatar
    Regular Contributor

    melanieKno in order for community members to provide the best solutions to your specific issue, in most cases, we need to analyze your code to determine what can be improved. Also remember that others with similar issues and varying skillsets may be referencing this post for a similar issue - so community responses are often written to cater to a larger audience and in a way that attempts to clearly define the inner workings of the problem and answer.


    That being said, it sounds like you'll need to add some extra logic within your try catch when you attempt to look for the object. I would suggest reviewing the documentation [here], as there are several methods of approach - IE: check the object's current state, check if it exists, wait for certain property values or state changes, etc.     

  • Kitt's avatar
    Regular Contributor

    I think you need to remove all of the ( ) from your xpath/css references. So instead of FindElement("(//div/input)[3]") you could try FindElement("//div/input/div[3]"). See the documentation on finding elements [here].


    I've never seen the approach of using parenthesis at the beginning of your xpath so I'm curious what you are using to obtain your object references? You should be able to use the Object Spy tool (or Object Browser) inside TestComplete to see if the element you are trying to find matches what is in your code. The SelectorsHub web extension is a handy tool that can help with testing xpath references better than TestComplete IMO.

  • rraghvani's avatar
    Champion Level 3

    Also note, FindElement method returns an object that matches the specified search criteria. If no matching object was found, the method returns an empty value i.e. None in Python. It doesn't raise an exception.


    The error is most likely relating to what Kitt has mentioned. You are performing a ClickButton() on buttonCreate object, whose value is null i.e. None in Python.


    TC will stop running, if an error occurs. As defined in Project Settings,




  • melanieKno's avatar
    Occasional Contributor

    I didn't ask for analysis of my code ;-), but anyway: the code is working as expected, when objects are available there's no issue. And even without parenthesis the log looks the same:


    When I doubleclick on the last line of the log it points to this line:


    There's no issue with the Create button, it's found without problems.


    So again my question: How can I handle this use-case (not knowing if objects are available or not while executing the code) when try-except does not catch the exception as expected?