IF-ELSE with Get Element Count not working

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

Hi @bk-user,

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

    ${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.

@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

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.

*** 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.

1 Like

@damies13

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

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

You could also use Exit For Loop but that is the old way of doing it and will be removed eventually.

Dave.

Thanks @damies13
Appreciate your quick help

1 Like