How to correctly run the TRY/EXCEPT syntax?

Try and except
    RPA.Browser.Selenium.Open Available Browser    https://newepco.dagangnet.com.my/dnex/dnex_app/#    #headless=${True}
    TRY
    RPA.Browser.Selenium.Wait Until Element Is Visible    css:#txt_usernameabc
    EXCEPT    Element 'Element css:#txt_usernameabc' not visible after 5 seconds.:
        Log    Not found
    EXCEPT    NoSuchWindowException:
        Log    No Window
    END

The above is my code for the try/except code, when the RPA.Browser.Selenium.Wait Until Element Is Visible css:#txt_usernameabc is failed, why it doesn’t go through the exception?

For robotframework:

A bigger difference is that with Python exceptions are objects 
and with Robot Framework you are dealing with error messages as strings.
By default matching an error using EXCEPT requires an exact match.

So you either need to use exact match or use other more flexible catching

http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#try-except-syntax

Thanks! Clearer now!

1 Like


Why after the TRY keyword fails, it doesn’t go to the except? As you can see the timeout is 3 mins, but the log file showing 4 mins and it doesn’t move to the next step. I have to manually close the browser by myself, to make the EXCEPT to continue to run.
and the Wait Until Element Is Visible css:#txt_fob_val timeout=3 mins fails is because out of memory, so if it’s out of memory i want the robot to close the browser:

Here is my code:

        TRY
            Wait Until Element Is Visible    css:#txt_fob_val    timeout=3 mins
        EXCEPT    
            Close Browser
            Start DNEX
            Second time
        ELSE
            Input Text    css:#txt_fob_val    ${excel_Total price}
            Click Element    css:#cmd_save_product_detail
        END

You are not sharing the full log, from the screenshot it shows that EXCEPT has been activated but received an error. So which keyword fails?

the except fails because i cancel the execution, as i mentioned the
Wait Until Element Is Visible css:#txt_fob_val timeout=3 mins
fails, and it doesn’t close the browser automatically, so i close the browser manually, it only then run the except execution, as i know the code has smthing wrong, so i cancel the execution, that’s why the except here shows failed

So if you are not manually closing browser execution just freezes and no timeout occurs for any of the keywords inside EXCEPT block? What do your keywords Start DNEX and Second time contain?

Yes, if i dont manually close the browser, the execution will freeze even out of the range of the timeout.
This is my code for Start DNEX
image

Even i try this:

The code still can’t move to the next step, i have to manually close the browser again, to let the code continue.


As u can see here, the time has over 3 mins, but the robot still freeze. This problem happens when the webpage has error:out of memory