How to skip to next step if element is not visible

i want to find element if it is visible click on element else go to other step and try to find that element.
ex:

if(driver.findelement(element1).click()){do this}
else
{do this}

but what happen in appium it does not go to else statement rather stays on if statement and try finding element whic is nnot available on screen
pls provide a solution ASAP!

Thanks in advance!!

Hey @s10v10s

driver.findElement() will return an “Unable to find element” exception if it can’t find an element, which halts the test instantly.

We are using an isExist() method that tries to find an element and suppresses the catched “Unable to find element” exception, and simply returns false.

isExist(Element){
try {
	driver.findelement(by);
	return true;
} catch (Exception e) {
	return false;
	}
}

There may be a better solution provided by Appium, (as some considers suppressing try-catch methods are wrong :sweat_smile: ) but I hope it might give an idea.

1 Like

You can also try findElements that returns an array:

if (driver.findElements(By.id("my_id")).size() > 0) {
    driver.findElement(By.id("my_id")).click();
    {do this}
} else {
    {do this}
}

It’s really up to the organization you want to give to your code, you should build your own abstract functions to see if an element is present or not.

@Telmo_Cardoso I tried but appium doesnot fetch any value rather be in waiting state for element to appear

It is not possible to use try and catch for every element , so there should be an another solution if element is not found in the specified time then it should jump to the next element.

I did’nt get it
I need a solution for below line means how should we achieve it
how should element be skipped to nxt element if not available on screen @Sukant

You have two solutions above… using try - catch and arrays. Both work. You can find your own solution, this as little to do with automation tool, its pure code problem.

Also make sure you are using latests appium version (1.7.1) and java client (5.0.4)

Hello All ,

Is there any way to record the video of running test cases on real device using Appium.

May be it will help you :
MobileElement e1= driver.findElementByName(“element1”);
try
{
if (e1.isenabled())
{
e1.click();

}

}

catch (NoSuchElementException e) {
 (do this)
}

Thanks, it really helped me out
https://www.rubydoc.info/github/appium/ruby_lib/Appium%2FDriver:exists