Struggling with handing an object if it exists, and ignoring it if it doesn't
I have a web page (non-mobile) that I'm trying to test, and it sometimes has a button that needs to be pressed. I'd like to have python code that works like "if the button exists, press it, otherwise just proceed." No errors needed, just that logic.
The button (when it exists) is here:
I've tried using WaitElement:
btnExists = True if Aliases.browser.pageName.formAspnetform.WaitElement(submitbuttonDelete,5).Exists else False
I've tried CheckProperty:
btn = aqObject.CheckProperty(Aliases.browser.pageName.formAspnetform.submitbuttonDelete, "value", cmpEqual, "Delete")
I've tried FindChild:
myBtn = Aliases.browser.pageName.formAspnetform.FindChild("submitbuttonDelete", "Delete*") if (myBtn.Exists): Log.Message("The button exists, so press it and wait") else: Log.Message("The button doesn't exist, nothing to do")
I must be missing something very simple, and am throwing in the towel after about 4 hours combing through the reference and googling. I'd rather not suppress logging around this. Any advice? Thanks!
Solved! Go to Solution.
You definitely need to use a WaitNNN method, but I think you need to try WaitNamedChild or WaitAliasChild since you are using name mapping.
You cannot use mapped names with WaitElement bc the object selector needs to be an XPath expression or a CSS selector. And the Find method cannot find an object that does not exists, so you'll have to use a Wait method in combination with the Exists property. Try:
def WaitAliasChildExample(): # Specifies the Alias object AliasObj = Aliases.browser.pageName.formAspnetform # Checks whether the submit button has appeared within 10 seconds if (AliasObj.WaitAliasChild("submitbuttonDelete", 10000).Exists): Log.Message("The submit button exists") else: Log.Error("The submit button does not exist")
Thank you. I ended up finally understanding that you need to reference the button's parent like you do with AliasObj. The rest looks very similar to yours and works now.