# Execute if/else when Element exists

**URL:** <https://forum.katalon.com/t/execute-if-else-when-element-exists/6216>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [May 17, 2017, 9:33am UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216 "2017-05-17T09:33:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roxanne\_Frankland](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Roxanne\_Frankland](https://forum.katalon.com/u/Roxanne_Frankland)\
**Post date:** [May 17, 2017, 9:33am UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/1 "2017-05-17T09:33:42Z")

</div>

I’m trying to test a page that randomly generates one of two objects each time the page is loaded. I’d like to use an if statement to execute a click action depending on which object is found. I tried this:

if (WebUI.verifyElementPresent(findTestObject(‘Objects/object1’)) == true) {  
WebUI.click(findTestObject('Objects/object1\_action))  
} else {  
WebUI.click(findTestObject(‘Objects/object2\_action’))  
}

where Objects/object1 is the id of one of the dynamically generated objects.

The problem with this is that if object1 is not found then the test fails. I need it to continue to the else block.

I’m probably doing something horribly wrong… hoping you can assist.

&nbsp;

---

<div class="post-metadata">

**Author:** ![Roxanne\_Frankland](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Roxanne\_Frankland](https://forum.katalon.com/u/Roxanne_Frankland)\
**Post date:** [May 18, 2017, 8:13am UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/2 "2017-05-18T08:13:54Z")

</div>

Yay it works!

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![Roxanne\_Frankland](https://avatars.discourse-cdn.com/v4/letter/r/2bfe46/32.png) [@Roxanne\_Frankland](https://forum.katalon.com/u/Roxanne_Frankland)\
**Post date:** [May 17, 2017, 3:36pm UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/3 "2017-05-17T15:36:46Z")

</div>

Thanks for your response. I amended the script as per your suggestion but am getting the following error when executing the test;

Test Cases/Test1 FAILED because (of) groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject, com.kms.katalon.core.model.FailureHandling) values: [TestObject - Object Repository/Objects/object1, …]

---

<div class="post-metadata">

**Author:** ![Prithviraj\_Sahoo](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@Prithviraj\_Sahoo](https://forum.katalon.com/u/Prithviraj_Sahoo)\
**Post date:** [May 17, 2017, 1:14pm UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/4 "2017-05-17T13:14:32Z")

</div>

I am also facing the same problem. Same with try catch blocks. The actions which are scripted in try block are not executed again after exception is handled via the catch block and I had to write the scripts for the actions in catch block as well. Hope, we get some suggestions.

---

<div class="post-metadata">

**Author:** ![Liubov\_Shalimova](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/liubov_shalimova/32/23870_2.png) [@Liubov\_Shalimova](https://forum.katalon.com/u/Liubov_Shalimova)\
**Post date:** [October 2, 2017, 3:44pm UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/5 "2017-10-02T15:44:42Z")

</div>

The same issue. Can anyone tell what to do?

---

<div class="post-metadata">

**Author:** ![duyluong](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/duyluong/32/1116_2.png) [@duyluong](https://forum.katalon.com/u/duyluong)\
**Post date:** [May 17, 2017, 3:49pm UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/6 "2017-05-17T15:49:09Z")

</div>

Sorry about my mistake. Follow this [link](https://docs.katalon.com/display/KD/%5BWebUI%5D+Verify+Element+Present), that keyword needs a timeout parameter.

---

<div class="post-metadata">

**Author:** ![duyluong](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/duyluong/32/1116_2.png) [@duyluong](https://forum.katalon.com/u/duyluong)\
**Post date:** [May 17, 2017, 1:12pm UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/7 "2017-05-17T13:12:52Z")

</div>

Katalon provides [FailureHandling](https://docs.katalon.com/display/KD/Failure+handling) feature as an parameter of every keywords to deal with failure. In your case, the script should be:

`if (WebUI.verifyElementPresent(findTestObject('Objects/object1'), FailureHandling.OPTIONAL)) {`  
`WebUI.click(findTestObject('Objects/object1_action))`  
`} else {`  
`WebUI.click(findTestObject('Objects/object2_action'))`  
`}`

---

<div class="post-metadata">

**Author:** ![Gowri\_Kumar\_Annapant](https://avatars.discourse-cdn.com/v4/letter/g/35a633/32.png) [@Gowri\_Kumar\_Annapant](https://forum.katalon.com/u/Gowri_Kumar_Annapant)\
**Post date:** [January 12, 2018, 4:49am UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/8 "2018-01-12T04:49:57Z")

</div>

Thanks “[duyluong](http://forum.katalon.com/profile/16/duyluong)” for the sample code.

---

<div class="post-metadata">

**Author:** ![Tim\_van\_der\_Weyden](https://avatars.discourse-cdn.com/v4/letter/t/c5a1d2/32.png) [@Tim\_van\_der\_Weyden](https://forum.katalon.com/u/Tim_van_der_Weyden)\
**Post date:** [January 18, 2018, 12:05pm UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/9 "2018-01-18T12:05:24Z")

</div>

I have used the same structure as given here, but it doesn’t work for me, the execution is not going into the else part of the code. It is frustrating me a little because searching on the web keeps bringing me here. I even tried to add ‘== true’ in the condition, but no luck.

---

<div class="post-metadata">

**Author:** ![Ajay\_Kumar\_Khare](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@Ajay\_Kumar\_Khare](https://forum.katalon.com/u/Ajay_Kumar_Khare)\
**Post date:** [January 18, 2018, 5:49pm UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/10 "2018-01-18T17:49:04Z")

</div>

How we can add assert condition in Katalon?

---

<div class="post-metadata">

**Author:** ![Mate\_Mrse](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mate_mrse/32/345_2.png) [@Mate\_Mrse](https://forum.katalon.com/u/Mate_Mrse)\
**Post date:** [February 19, 2018, 9:50am UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/11 "2018-02-19T09:50:06Z")

</div>

I’m having the same problem as @“Tim van der Weyden”. I tried all of the above solutions, but none worked.  
Any progress?

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [May 24, 2018, 3:31am UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/12 "2018-05-24T03:31:18Z")

</div>

Expanding on the answer from [duyluong](http://forum.katalon.com/profile/16/duyluong) and putting it all together:

```auto
// Set how long to wait to interact with/verify an element on the screen.// Setting this to 0 seems to fall back to the project default, which could be 30 sec.// If you don't want to wait that long, set to an integer > 0.int timeout = 1// We can define the test objects first for re-use.// This does not actually try to find the object on the page, but only looks// for the definition of the object in the object repository.TestObject object1 = findTestObject('Objects/object1_action')TestObject object2 = findTestObject('Objects/object2_action')// FailureHandling.STOP_ON_FAILURE = Default behavior. Exits/fails the test when the element is not found// FailureHandling.CONTINUE_ON_FAILURE = reports that an error occurred during the run but doesn't exit; continues running the "else" clause// FailureHandling.OPTIONAL = no error reported; continues running the "else" clause
if (WebUI.verifyElementPresent(object1, timeout, FailureHandling.OPTIONAL)) {
    WebUI.click(object1)
} else {
    WebUI.click(object2)
}

```

---

<div class="post-metadata">

**Author:** ![d11](https://avatars.discourse-cdn.com/v4/letter/d/47e85d/32.png) [@d11](https://forum.katalon.com/u/d11)\
**Post date:** [July 13, 2018, 11:52am UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/13 "2018-07-13T11:52:00Z")

</div>

thanks for the answers.

Note however that **Katalon Studio freezes up if I use a high TIMEOUT value (300 secs).**

---

<div class="post-metadata">

**Author:** ![Uriel\_Verstegen](https://avatars.discourse-cdn.com/v4/letter/u/aeb1de/32.png) [@Uriel\_Verstegen](https://forum.katalon.com/u/Uriel_Verstegen)\
**Post date:** [August 2, 2018, 2:50pm UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/14 "2018-08-02T14:50:41Z")

</div>

I’m following the same line of code, but it won’t let me use Else with my If statement. It gives me the error ‘expecting ‘}’, found ‘else’ @ line 144, column 15.’

But it wants me to put enough brackets there to end my entire block, and then I can’t have my else statement. Any thoughts? I can try to provide some code

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [August 5, 2018, 7:05pm UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/15 "2018-08-05T19:05:01Z")

</div>

Hi Uriel,

A code sample would be helpful to figure out what’s going on.

At its simplest, an if/else block looks like:

```auto
if (logic) {
   do the true thing
} else {
    do the false thing
}

```

It’s possible that if you have complicated “logic” statements, that you forgot to close a “)” at the end of it. Also note the opening "{"at the end of the “if” line.

Some people find if/else statements easier to write out with more line breaks, just to be more clear:

```auto
if (logic) 
{
   do the true thing
} 
else 
{
    do the false thing
}

```

---

<div class="post-metadata">

**Author:** ![Uriel\_Verstegen](https://avatars.discourse-cdn.com/v4/letter/u/aeb1de/32.png) [@Uriel\_Verstegen](https://forum.katalon.com/u/Uriel_Verstegen)\
**Post date:** [August 9, 2018, 1:19pm UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/16 "2018-08-09T13:19:23Z")

</div>

It was as simple as missing a closing parenthesis, Chris. Thank you for pointing that out. I thought I had checked it.

---

<div class="post-metadata">

**Author:** ![Vasudha\_Vanol1](https://avatars.discourse-cdn.com/v4/letter/v/ce73a5/32.png) [@Vasudha\_Vanol1](https://forum.katalon.com/u/Vasudha_Vanol1)\
**Post date:** [October 9, 2018, 11:52pm UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/17 "2018-10-09T23:52:52Z")

</div>

Hi,

I am facing same problem in this scenario. I used above code in my script but not succeed.

In my case; I have to search object 1 in the page and if object 1 is found than click on it and pop-up window will open to do next step. if object 1 is not found and object 2 is available in the page on that time have to click on object 2 and than click on object 3 and than after pop-up window will open to do next step.

Here, What happen in first case;

Object 1 found and click on it and pop-up window open and then script go to search object 2 and it is not available on pop-up window and throw error message.

Second Case:

When object 1 is not found in the page on that time script doesn’t switch to else statement but trying to search object 1 and script doesn’t stop at all.

FYI: Project Settings \>\> Execution \>\>Default

Default wait for element timeout (in seconds) = 3  
Delay between actions (in secs) = 1  
Default page load time out = wait until the page is loaded

For second case, I try with 30 secs time limit for wait for element

Code:

int timeout = 1

TestObject object1 = findTestObject(‘Object Repository/Object 1’)

TestObject object2 = findTestObject(‘Object Repository/Object 2’)

if (WebUI.verifyElementPresent(object1, timeout, FailureHandling.OPTIONAL)){

WebUI.click(object1)

} else {

WebUI.click(object2)

```
WebUI.click(findTestObject('Object Repository/Object 3'))

```

}

‘Click on add new user’

WebUI.click(findTestObject(‘Object Repository/Object 4’))

Error Message:

10-10-2018 10:30:26 AM - [FAILED] - Unable to click on object ‘Object Repository/ClientPreManagement-Object 2)’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/ClientPreManagement- Object 2’ located by ‘By.xpath: //\*[@id = ‘xpath’]’ not found)

10-10-2018 10:30:26 AM - [FAILED] - Test Cases/Client Pre-Management Module - 400/UAT-LL-470-Add or edit a client Finance Manager/Reject\_Sub\_ClientFinmgr FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: Unable to click on object ‘Object Repository/object 2’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/object 2’ located by ‘By.xpath: //\*[@id = xpath]’ not found)

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [October 10, 2018, 2:12am UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/18 "2018-10-10T02:12:27Z")

</div>

Hi Vasudha,

The error about not being able to find Object 2 with an xpath of “//\*[[@id](http://forum.katalon.com/profile//id "Link: http://forum.katalon.com/profile//id") = xpath]” tells me that the object might not be set up correctly in the Object Repository. Could you please share what Object 1 and Object 2 look like in the repository?

Thanks,

Chris

---

<div class="post-metadata">

**Author:** ![Vasudha\_Vanol1](https://avatars.discourse-cdn.com/v4/letter/v/ce73a5/32.png) [@Vasudha\_Vanol1](https://forum.katalon.com/u/Vasudha_Vanol1)\
**Post date:** [October 11, 2018, 2:40am UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/19 "2018-10-11T02:40:44Z")

</div>

Hi Chris,

Thanks for prompt response.

For First Case- Error Message: Once Object 1 is found than also script searching for Object 2 and throw error message

10-11-2018 01:31:03 PM - [FAILED] - Unable to click on object ‘Object Repository/ClientPreManagement-400/ClientPage/NewManagerRoles/Page\_ClientDetails/input\_AddnewUser’ (Root cause: org.openqa.selenium.ElementNotVisibleException: element not visible

10-11-2018 01:31:04 PM - [FAILED] - Test Cases/Client Pre-Management Module - 400/UAT-LL-471-Add or edit a Client Manager/Add\_Edit\_ClientManager FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/ClientPreManagement-400/ClientPage/NewManagerRoles/Page\_ClientDetails/3dots\_ClientManager’ located by ‘By.xpath: //\*[@id = ‘LoopedIn\_Theme\_wt5\_block\_wtMainContent\_Clients\_WEB\_wt111\_block\_wt10\_VVITFramework\_WEB\_wt1\_block\_wtlblToggle’]’ not found

For Second Case - Error Message: Script searching for Object 1 if its not found than it should search Object 2 but here it stuck to find object 1 and failed

10-11-2018 09:46:31 AM - [FAILED] - Test Cases/Client Pre-Management Module - 400/UAT-LL-471-Add or edit a Client Manager/Add\_Edit\_ClientManager FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/ClientPreManagement-400/ClientPage/NewManagerRoles/Page\_ClientDetails/input\_ClientManager’ located by ‘By.xpath: id(“LoopedIn\_Theme\_wt5\_block\_wtMainContent\_Clients\_WEB\_wt111\_block\_wt10\_wt4”)[count(. | //input[@type = ‘submit’ and @name = ‘LoopedIn\_Theme\_wt5$block$wtMainContent$Clients\_WEB\_wt111$block$wt10$wt4’ and @id = ‘LoopedIn\_Theme\_wt5\_block\_wtMainContent\_Clients\_WEB\_wt111\_block\_wt10\_wt4’]) = count(//input[@type = ‘submit’ and @name = ‘LoopedIn\_Theme\_wt5$block$wtMainContent$Clients\_WEB\_wt111$block$wt10$wt4’ and @id = ‘LoopedIn\_Theme\_wt5\_block\_wtMainContent\_Clients\_WEB\_wt111\_block\_wt10\_wt4’])]’ not found

---

<div class="post-metadata">

**Author:** ![Prashant\_Pednekar1](https://avatars.discourse-cdn.com/v4/letter/p/c67d28/32.png) [@Prashant\_Pednekar1](https://forum.katalon.com/u/Prashant_Pednekar1)\
**Post date:** [December 11, 2018, 1:51am UTC](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216/20 "2018-12-11T01:51:41Z")

</div>

Hi Guys, how do we make sure that Failure is not reported when it goes to the Else loop? It somehow frightens the users that something failed when it should not be the case.

Also cannot enter the else loop without putting a CONTINUE\_ON\_ERROR failure handling

if (WebUI.verifyElementPresent(findTestObject(‘Object Repository/Page\_PM - Management/span\_Error\_Message’), 6))  
{  
String ErrorMessage = WebUI.getText(findTestObject(‘Object Repository/Page\_PM - Management/span\_Error\_Message’))  
KeywordLogger log = new KeywordLogger()  
log.logInfo(‘Creation of Contact failed for the new Contact with Error :’ + ErrorMessage)  
}  
else   
{   
KeywordLogger log = new KeywordLogger()  
log.logInfo(‘Creation of Contact Passed’)  
}

[Next page](https://forum.katalon.com/t/execute-if-else-when-element-exists/6216.md?page=2)
