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
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}
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
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