Reputation: 117
I am automating a android app with Appium using Java. My scenario is, I need to click either button 1 or button 2, whichever is present
Appium error log:[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying. [debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding '//android.widget.ImageButton[@resource-id='net.ilius.android.meetic:id/profileMailPremiumButton']' using 'XPATH' with the contextId: '' multiple: false
if (driver.findElementByXPath("//android.widget.ImageButton[@resource-id='net.ilius.android.meetic:id/profileMailPremiumButton']")
.isDisplayed()) {
driver.findElementByXPath("//android.widget.ImageButton[@resource-id='net.ilius.android.meetic:id/profileMailPremiumButton']")
.click();
} else {
driver.findElementById("net.ilius.android.meetic:id/profileMailButton").click();
}
Upvotes: 1
Views: 442
Reputation: 2770
If you use the isDisplayed() and element is not present on the UI it will throw the exception - element not found.
So instead of that first check whether that element is exist or not by using the findElements : driver.findElements(selector).isEmpty()
if it is empty it means element is not available now you can go to the else block
Use this piece of code : `
if (!driver.findElements(By.xPath("//android.widget.ImageButton[@resource-id='button1']")).isEmpty()) {
driver.findElementByXPath("//android.widget.ImageButton[@resource-id='button1']").click();
} else {
driver.findElementById("button2").click();
}`
Upvotes: 1