[Solved] Make a condition on a button

Hi,
I would like to make a smart test with the presence (or not) of a button on the app.
For example:
If (button is on the screen)
Do task 1
Else
Do task 2

But I don’t know if a function existe to know if the button is on the screen or not
Is it possible to make this?

Hi,
You need to export the code into Java and perform this logic in Java. It should look something like:
try {
driver.findElement();
// Do task 1
} catch (Exception e){
// Do task 2
}

I use this to validate item, but I code in C# just adapt to Java or other language:

try {
    OpenQA.Selenium.Support.UI.WebDriverWait wait =
        new OpenQA.Selenium.Support.UI.WebDriverWait(driver, new TimeSpan(0, 0, 30));

    wait.Until(
      OpenQA.Selenium.Support.UI.ExpectedConditions.
        VisibilityOfAllElementsLocatedBy(By.XPath("xpath_of_element")));

    // Element is found, everything is OK!
 
}
catch (Exception ex) { 
    // Element don't exist, use plan B
}

thanks for your help :slight_smile:

Sorry but the source code I pasted here doesn’t work fine, I rewrite it from scratch and now works like a lightning storm :zap:

In my case, the wait.Until sentence doesn’t detect the element I was looking for and when the timeout is reached it exits without throwing an exception

The new code makes the job in only 2.8seconds (the time that the APP gets open):

in C#:

while (miCrono.ElapsedMilliseconds < configuracion.script_TimeOut)
{
  try
  {
    AppiumWebElement item;
    item = configuracion.appium_Driver.FindElementByXPath("xpath_of_the_element");
    if (item != null) break;
  }
  catch (Exception ex) { throw new Exception("2"); }
}