Unable to verify IF element present or not

I want to click a button if the button is present. if it is not available move to the next step and proceed. Tried the below step

if (WebUI.verifyElementPresent(findTestObject(‘Object Repository/btn_AddNew’), 10)==true) {
WebUI.click(findTestObject(‘Object Repository/btn_AddNew’))
}

Script fails at the if conditional statement

2019-01-29 16:12:15.537 e[34mINFO e[0;39m e[36mc.k.k.c.webui.common.WebUiCommonHelper   -e[0;39m e[39mUnable to find the element located by 'By.xpath: //input[@type = 'submit' and @name = 'ctl00$TRNContentPlaceHolder$btnAdd' and @id = 'TRNContentPlaceHolder_btnAdd']'. Please recheck the objects properties to make sure the desired element is located. e[0;39m
2019-01-29 16:12:16.439 e[1;31mERRORe[0;39m e[36mc.k.k.core.keyword.internal.KeywordMain  -e[0;39m e[31m❌ com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/btn_AddNew' located by 'By.xpath: //input[@type = 'submit' and @name = 'ctl00$TRNContentPlaceHolder$btnAdd' and @id = 'TRNContentPlaceHolder_btnAdd']' not founde[0;39m
2019-01-29 16:12:16.951 e[1;31mERRORe[0;39m e[36mc.k.k.core.keyword.internal.KeywordMain  -e[0;39m e[31m❌ Unable to verify object 'Object Repository/btn_AddNew' is present (Root cause: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/TAS/Page_Transportation Asset System -/btn_AddNew' located by 'By.xpath: //input[@type = 'submit' and @name = 'ctl00$TRNContentPlaceHolder$btnAdd' and @id = 'TRNContentPlaceHolder_btnAdd']' not found)e[0;39m
2019-01-29 16:12:17.000 e[1;31mERRORe[0;39m e[36mc.k.k.core.main.RawTestScriptExecutor    -e[0;39m e[31m❌ if (verifyElementPresent(findTestObject("Object Repository/btn_AddNew"), 10) == true) FAILED.e[0;39m
e[31mReason:e[0;39m
e[31mcom.kms.katalon.core.exception.StepFailedException: Unable to verify object 'Object Repository/btn_AddNew' is present (Root cause: com.kms.katalon.core.exception.StepFailedException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/btn_AddNew' located by 'By.xpath: //input[@type = 'submit' and @name = 'ctl00$TRNContentPlaceHolder$btnAdd' and @id = 'TRNContentPlaceHolder_btnAdd']' not found)e[0;39m
e[31m	at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy:36)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:65)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:27)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword.verifyElementPresent(VerifyElementPresentKeyword.groovy:92)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.builtin.VerifyElementPresentKeyword.execute(VerifyElementPresentKeyword.groovy:68)e[0;39m
e[31m	at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:53)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyElementPresent(WebUiBuiltInKeywords.groovy:1434)e[0;39m
e[31m	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$verifyElementPresent.call(Unknown Source)e[0;39m

The verifyElementPresent() method acts as an assertion, which means that if the method returns false, the test will stop, and the if statement won’t even be evaluated. The solution is to add a FailureHandling argument to the verifyElementPresent() method call. See the solution to a similar topic here:

Hi everyone,

We have already fixed the bug that caused the verifyElementPresent() and verifyElementNotPresent() keywords to throw an exception instead of returning false when the verified elements do not exist.

Please first check out the release note and click here to download if you want to try version 7.0.0 (beta) in advance.

Happy Testing!

Jass

1 Like

This exception is still appearing on Katalon 7.4.3.

Hi Jass,

We are using 7.5.5 version of Katalon but still facing some issue around verifyElementPresent property. In the example below: On first loop “Button1” and “Button2” exists while in 2nd only “Button2” exists. But Execution stops returning couldn’t find “Button1” with xpath=‘id=Button1’

for (int i = 1; i <= 2; i++) {
if (WebUI.verifyElementPresent(findTestObject(‘Automation/Button1’), 1) == true) {
WebUI.click(findTestObject(‘Automation/Button1’)) }
else if (WebUI.verifyElementPresent(findTestObject(‘Automation/Button2’), 1) == true) {
WebUI.click(findTestObject(‘Automation/Button2’))
} else {}

}

Could you please look into and help to fix?=

@vishal.gaikwad

Hi, please add a FailureHandling.CONTINUE_ON_FAILURE as the second parameter of the click(s).

WebUI.click(findTestObject(‘Automation/Button1’), FailureHandling.CONTINUE_ON_FAILURE)

@ThanhTo Thank yo for your prompt reply. We have already tried to use FailureHandling, but unfortunately returns same error. Could you please help to debug further? If you need any more details, feel free to tag me.

They actually need to add failure handling to the verifyElementPresent() calls, not the click() calls.

@vishal.gaikwad try this:

for (int i = 1; i <= 2; i++) {
    if (WebUI.verifyElementPresent(findTestObject(‘Automation/Button1’), 1, FailureHandling.CONTINUE_ON_FAILURE)) {
        WebUI.click(findTestObject(‘Automation/Button1’)) 
    }
    else if (WebUI.verifyElementPresent(findTestObject(‘Automation/Button2’), 1, FailureHandling.CONTINUE_ON_FAILURE)) {
        WebUI.click(findTestObject(‘Automation/Button2’))
    }
}

If FailureHandling.CONTINUE_ON_FAILURE doesn’t work, please try FailureHandling.OPTIONAL.

Ah my bad, I didn’t see that important statement. Thanks !

@Brandon_Hein @ThanhTo Thank you both of you, FailureHandling.CONTINUE_ON_FAILURE worked :slight_smile:

1 Like

Hi All, but even with using the failure handling set to continue, Still the test case is considered as a failed test case, which harms my reports and the velocity of the test suite, need urgent assistance with this matter. Thanks