# IF-ELSE with Get Element Count not working

**URL:** <https://forum.robotframework.org/t/if-else-with-get-element-count-not-working/5341>\
**Category:** Robot Framework\
**Created:** [21 February 2023 08:37 UTC](https://forum.robotframework.org/t/if-else-with-get-element-count-not-working/5341 "2023-02-21T08:37:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bk-user](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/bk-user/32/1446_2.png) [@bk-user](https://forum.robotframework.org/u/bk-user)\
**Post date:** [21 February 2023 08:37 UTC](https://forum.robotframework.org/t/if-else-with-get-element-count-not-working/5341/1 "2023-02-21T08:37:52Z")

</div>

Hi

I have written Code like below to check element count. If Count is 0 then check for other element count using XPATH. Currently it is checking count for all the element. I want to check in else block only if count is 0 in If block

```
 ${count} = SeleniumLibrary.Get Element Count ${random_errors_xpath}
 ${count1} = SeleniumLibrary.Get Element Count ${error1_site_can_not_be_reached_xpath}

    IF ${count}>0
         ${error_text}= Get Error Text ${random_errors_xpath}
    ELSE IF ${count1}>0
         ${error_text}= Get Error Text ${error1_site_can_not_be_reached_xpath}
   END

```

I want try like this but not working

```
    IF SeleniumLibrary.Get Element Count ${random_errors_xpath}>0
         ${error_text}= Get Error Text ${random_errors_xpath}
    ELSE IF SeleniumLibrary.Get Element Count ${error1_site_can_not_be_reached_xpath}>0
         ${error_text}= Get Error Text ${error1_site_can_not_be_reached_xpath}
   END

```

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [21 February 2023 14:47 UTC](https://forum.robotframework.org/t/if-else-with-get-element-count-not-working/5341/2 "2023-02-21T14:47:27Z")

</div>

Hi @bk-user,

If I understood what your asking then it sounds like you want something like this:

```python
    ${count}= SeleniumLibrary.Get Element Count ${random_errors_xpath}

    IF ${count}>0
        ${error_text}= Get Error Text ${random_errors_xpath}
    ELSE
        ${count1}= SeleniumLibrary.Get Element Count ${error1_site_can_not_be_reached_xpath}
        IF ${count1}>0
            ${error_text}= Get Error Text ${error1_site_can_not_be_reached_xpath}
        END
    END

```

Also you need to watch your indentation, you had 4 spaces before the `IF` and `ELSE` but only 3 spaces before the `END`. Like Python the whitespace (spaces and tabs matter and can cause unexpected results if you’re not careful with them.

Dave.

---

<div class="post-metadata">

**Author:** ![bk-user](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/bk-user/32/1446_2.png) [@bk-user](https://forum.robotframework.org/u/bk-user)\
**Post date:** [22 February 2023 04:54 UTC](https://forum.robotframework.org/t/if-else-with-get-element-count-not-working/5341/3 "2023-02-22T04:54:03Z")

</div>

@damies13  
Thanks for answers  
I have to check with IF -ELSE-IF because I have 10 XPath to check like  
IF  
ELSE IF  
ELSE IF  
ELSE IF  
ELSE IF  
.  
.  
.  
END

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [22 February 2023 14:16 UTC](https://forum.robotframework.org/t/if-else-with-get-element-count-not-working/5341/4 "2023-02-22T14:16:10Z")

</div>

Hi @bk-user,

The problem with the IF-ELSE-IF-ELSE-IF structure like you have is you’ll need to evaluate all 10 xpath’s in advance, so how about a different approach?

We put the xpath’s you need to check in a list in the order you want to check them, then use a for loop to iterate over them checking them one at a time, then use a break to exit the loop when you find one.

```python
***Variables***
@{XPATHS} //xpath/one //xpath/two
... //xpath/three //xpath/four 
... //xpath/five //xpath/six
... //xpath/seven //xpath/eight 
... //xpath/nine //xpath/ten

***Test Cases***
Check Xpaths In Page
    FOR ${xpathtocheck} IN @{XPATHS}
        ${count}= SeleniumLibrary.Get Element Count ${xpathtocheck}
        IF ${count}>0
            ${error_text}= Get Error Text ${xpathtocheck}
            BREAK
        END
    END

```

Dave.

---

<div class="post-metadata">

**Author:** ![bk-user](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/bk-user/32/1446_2.png) [@bk-user](https://forum.robotframework.org/u/bk-user)\
**Post date:** [23 February 2023 03:53 UTC](https://forum.robotframework.org/t/if-else-with-get-element-count-not-working/5341/5 "2023-02-23T03:53:51Z")

</div>

@damies13

I like this approach but I am getting below error for BREAK statement

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/f/fa971b879a300b264ac03237eaca455456ac9e14.png)

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [23 February 2023 04:14 UTC](https://forum.robotframework.org/t/if-else-with-get-element-count-not-working/5341/6 "2023-02-23T04:14:09Z")

</div>

Hi @bk-user,

That must be an issue with your editor’s syntax highlighting system, Here’s the reference to the `BREAK` command in the manual:

[Loop control using `BREAK` and `CONTINUE`](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#toc-entry-380)

You could also use [Exit For Loop](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Exit%20For%20Loop) but that is the old way of doing it and will be removed eventually.

Dave.

---

<div class="post-metadata">

**Author:** ![bk-user](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/bk-user/32/1446_2.png) [@bk-user](https://forum.robotframework.org/u/bk-user)\
**Post date:** [23 February 2023 05:57 UTC](https://forum.robotframework.org/t/if-else-with-get-element-count-not-working/5341/7 "2023-02-23T05:57:34Z")

</div>

Thanks @damies13  
Appreciate your quick help
