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