If - elseif - else - endIf (expression, label) - Selenium IDE command

If is the granddaddy of all conditionals and is available in the UI.Vision RPA Selenium IDE. It allows the macro, for example, to react to errors and changes on the website.

How to use if/elseif/else/end: Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. With if/elseif/else/end you use if to specify a block of code to be executed, if a specified condition is true and use else to specify a block of code to be executed, if the same condition is false. The expression is technically a Javascript EVAL, exactly like in executeScript_Sandbox.

Related user questions:

1. When I run the macro sometimes I get [error] time out when looking for element 'css=button.btn.btn-primary....'. .Is there a way to ignore these errors and go to next step? Like "if (notFound) go to next step".

2. Hi, Sometimes when a macro is playing it encounters an error and then it will not be completed (for example the image will not be found). Is there a way to override such errors? Like for example: 1. Start over. 2. Go to the next line. 3. Go to a specified line.

Solution for 1 and 2: The solution is to use if together with the internal variable !statusOK. You also need to set !errorIgnore to true, so the macro execution does not stop.
Example: If | ${!statusOK}...else....end or gotoIf | ${!statusOK} | Label.

The above code goes into if...end if the there was no error before. But if you want to do something if the last command was NOT successful (element NOT found etc), then you can simply add the "!" (Javasacript not operator) in front of the internal variable.
Example: If | !${!statusOK} is the same as If | ${!statusOK} == false.
The extra ! in front of the internal variable is the Javascript symbol for NOT as in "!(x == y) is true".

If uses executeScript_Sandbox

UI Vision Selenium IDE uses executeScript_Sandbox to evaluate an expression.

If Example

The example macro tries a second link if the first one is not found. Best if you see the flow logic in action by running the DemoIfElse macro that installs with the IDE.

Command Target Pattern/Text
open https://ui.vision/
store true !errorIgnore
click link=helloworld
If ${!statusOK} == true
click link=anotherLink
echo The first link was found

If ${!statusOK} == true

Note that !statusOK is a boolean value, so there are no "" around true or false:

RPA boolean values

Do...RepeatIf and While...End

If you want to use IF as condition for loop, consider using do... repeat if (condition) or while (condition)... end.

If IMAGE do THIS: This screencast uses repeat if to wait for an image, and do something once it appears.

Works in

UI.Vision RPA for Chrome Selenium IDE, UI.Vision RPA for Firefox Selenium IDE, Firefox IDE Classic

Related Demo Macros

DemoGotoIf, DemocsvSave

The ready-to-import-and-run source code of all demo macros can be found in the Open-Source RPA software Github repository.

See also

gotoIf, gotoLabel, Label, while, endWhile, !LastCommandOK, !errorIgnore, Web Automation Extension User Manual, Selenium IDE commands, Selenium IDE Flow Control (SelBlocks), Classic Firefox Selenium IDE.

Anything wrong or missing on this page? Suggestions?

...then please contact us.

<em>UI.Vision RPA</em> Selenium IDE for Chrome and Firefox - Web Test Automation
← Meet the Ui.Vision team and users on our RPA software forum.