Check if element exists (error)

I try to check if an element does exist on the page and if not an error message is printed:

if(WebUI.waitForElementPresent(to, 3, FailureHandling.OPTIONAL)==false){     KeywordUtil.markFailedAndStop(subtopic+' '+item+' option not available. (attribute given: '+detail+' '+attribute+')')}

However the test does not stop, even if the TestObject ‘to’ is not present, because of the following:

10-01-2018 01:18:50 PM - [INFO]   - Finding web element with id: 'Object Repository/bm/pkg/pkgselected' located by 'By.xpath: //*[@class='ng-scope selected']//div[@class='pack S0233']' in '10' second(s)10-01-2018 01:19:00 PM - [INFO]   - Web element found by trial and error method is not used because we can't guarantee it reflects the intended element.10-01-2018 01:19:00 PM - [FAILED] - Web element with id: 'Object Repository/bm/pkg/pkgselected' located by 'By.xpath: //*[@class='ng-scope selected']//div[@class='pack S0233']' not found10-01-2018 01:19:00 PM - [FAILED] - Unable to verify object 'Object Repository/bm/pkg/pkgselected' is visible (Root cause: Web element with id: 'Object Repository/bm/pkg/pkgselected' located by 'By.xpath: //*[@class='ng-scope selected']//div[@class='pack S0233']' not found)

I am not sure if this error is already known, but does anyone know workaround for checking an elements existence ?

hello,
1st - shortcut for if() statement

if(!WebUI.waitForElementPresent(to, 3, FailureHandling.OPTIONAL)){
     KeywordUtil.markFailedAndStop(subtopic+' '+item+' option not available. (attribute given: '+detail+' '+attribute+')')
}

2nd
try to check for elements in class separatly:

//*[@scope selected']//div[contains(@class,'pack') and contains(@class,'S0233')]

Hi,
thank you for the shortcut, I will use it this way

I used your suggestion and sepereated it but the error still appears.

10-01-2018 02:16:07 PM - [INFO]   - Finding web element with id: 'Object Repository/bm/pkg/pkgselected' located by 'By.xpath: //*[@class='ng-scope selected']//div[contains(@class,'pack') and contains(@class,'S0233')]' in '10' second(s)10-01-2018 02:16:17 PM - [INFO]   - Web element found by trial and error method is not used because we can't guarantee it reflects the intended element.

can you please post html that contains element you want to use

But I think you miss the point, i want to check if an element exists, even if it does not.

At this point the class ‘ng-scope’ is from an option that can be selected. When selected it changes to ‘ng-scope selected’.

So I try to to check if the option is selected with the following command:

if(WebUI.waitForElementPresent(to, 3, FailureHandling.OPTIONAL)==false){     KeywordUtil.markFailedAndStop(subtopic+' '+item+' option not available. (attribute given: '+detail+' '+attribute+')')}

Where TestObject ‘to’ is: //*[@class=‘ng-scope selected’]//div[contains(@class,‘pack’) and contains(@class,‘S0233’)]

So if it does not find the ‘ng-scope selected’ class I can say the option is not selected/available.

kat3.PNG

now i think i get it …
my guess: data-h5vco-panel-package is unique enough to identify TO
then define xpath of TO as //div[(at)data-h5vco-panel-package] for any TO you can get all attributes by calling WebUI.getAttribute( TO, ‘class’) function and then process attributes that are returned
due to forum bug, repace ‘(at)’ with ‘@’

Thank you, I will try that and comment here how it worked out :slight_smile:

Edit: Works perfectly for all cases so far; can be closed