How to set variable as 0 if there is no matching XPATH

I want to print text from different boxes in a column but sometimes there will be boxes present sometimes not (i.esometimes there will be matching xpath and sometimes there will be no xpath)

Currently I’m using FOR loop to print values from the boxes column wise if there is no matching xpath it should convert it as 0

Currently I have my code as:

@{list}  Create List
FOR  ${index}  IN RANGE 1 ${columns}
    ${success}=  Run Keyword And Return Status  Element Should Be Visible  ${xpath}
    ${elements}=  Run Keyword If  ${success}  Get WebElements  ${xpath}
    ${texts}=  Run Keyword If  ${elements}  Get Text  @{elements}  ELSE  0
    Append To List  ${list}  ${texts}
END

Issue with the code is that it is printing only 1 text but I want to print all the text which is stored in @{elements}
as i’m using for loop to change index position in xpath sometimes there will be no matching xpath as sometimes there will be boxes present in columns sometimes not
anyone can help me to solve this issue

1 Like

I got error as:
Get Text expected 1 argument, got 8

 ${texts}=  Run Keyword If  ${elements}  Get Text  @{elements}  ELSE  0

You need to iterate in the elements list.
Here is a possible solution:

*** Settings ***
Library           Collections
Library           SeleniumLibrary

*** Test Cases ***
Example Test
    My Get Text

*** Keywords ***
My Get Text
    @{list}    Create List
    FOR    ${index}    IN RANGE    1    ${columns}
        ${success}=    Run Keyword And Return Status    Element Should Be Visible    ${xpath}
        ${elements}=    Run Keyword If    ${success}    Get WebElements    ${xpath}
        Extract Text    ${elements}
    END

extract text
    [Arguments]    ${elements}
    ${size}=    Get Length    ${elements}
    ${texts}=    Set Variable    ${EMPTY}
    IF    ${size}>0
        FOR    ${element}    IN    @{elements}
            ${t}=    Get Text    ${element}
            ${texts}=    Catenate    ${texts}    ${t}
        END
    ELSE
        ${texts}=    Set Variable    0
    END
    Append To List    ${list}    ${texts}
1 Like

@HelioGuilherme66 Thanks for the solution and your efforts.

As there will be different types of boxes so xpath for them will be different so above keyword will work for particular test case only right? (as xpath will be different i cant use this in other test cases)
Is there any way that we can do this without using keywords?
TIA

I tried it by
@{list} Create List
FOR ${index} IN RANGE 1 ${columns}
${success}= Run Keyword And Return Status Element Should Be Visible ${xpath}
${elements}= Run Keyword If ${success} Get WebElements ${xpath}
Extract Text ${elements}
END

By doing this but got error as Else a reserved keyword . it must be an upper case ‘ELSE’ and follow an opening IF when used as marker

my code was already in IF statement then I used above code I double crosschecked code but there is no Else written anywhere in code
I dont know how to tackle this situation

Well, your initial code has an error in the ELSE 0. It should have been ELSE Set Variable 0.

In the code you tried, I don’t see an ELSE.

You don’t mention, but I hope you are using a recent Robot Framework, like 6.0.2.

My code is

IF  ${is_enabled}
  @{list}  Create List
  FOR  ${index}  IN RANGE 1 ${columns}
      ${success}=  Run Keyword And Return Status  Element Should Be Visible  ${xpath}
      ${elements}=  Run Keyword If  ${success}  Get WebElements  ${xpath}
      ${texts}=  Run Keyword If  ${elements}  Get Text  @{elements}  ELSE   Set Variable 0
      Append To List  ${list}  ${texts}
   END
ELSE
     Log   element disabled
     Skip

This code I was using earlier

IF  ${is_enabled}
  @{list} Create List
  FOR ${index} IN RANGE 1 ${columns}
    ${success}= Run Keyword And Return Status Element Should Be Visible ${xpath}
    ${elements}= Run Keyword If ${success} Get WebElements ${xpath}
    Extract Text ${elements}
  END
ELSE
     Log   element disabled
     Skip
 

tried By doing this but got error as Else a reserved keyword . it must be an upper case ‘ELSE’ and follow an opening IF when used as marker

@HelioGuilherme66 Can we do this without using keywords?