Skip the item that doesn't exist

Hybrid application
I have 3 items
When logging in, sometimes there is an item that does not exist. I want to correct the error and not stop the program

MobileElement el2 = (MobileElement) driver.findElementByAccessibilityId(“1”);
el2.click();
MobileElement el2 = (MobileElement) driver.findElementByAccessibilityId(“2”);
el2.click();
MobileElement el2 = (MobileElement) driver.findElementByAccessibilityId(“3”);
el2.click();

If element number 1 does not exist, I want it to read element number 2, and if it does not exist, it reads number 3 and don’t stop program

wrap your each findElement inside if condition, perform operation only when true. like
if ((MobileElement) driver.findElementByAccessibilityId(“1”)){
(MobileElement) driver.findElementByAccessibilityId(“1”);
el2.click();
}

1 Like

Sorry, I wrote wrong code
Please write the command for the following code in full

	MobileElement el1 = (MobileElement) driver.findElementByXPath("1");
	el1.click();

	MobileElement el2 = (MobileElement) driver.findElementByAccessibilityId("2");
	el2.click();

	MobileElement el3 = (MobileElement) driver.findElementByXPath("3");
	el3.click();

	MobileElement el4 = (MobileElement) driver.findElementById("4");
	el4.click();