Explicitly wait until visibility of an element using Android driver in Java

Hello,

I have recently started using ‘appium’ to automate an android app by using Java.
Is there a way by which I can wait until an element is displayed on the mobile screen ?

For example: I am using the following code for web automation to achieve the similar functionality
WebElement element;
WebDriverWait wait = new WebDriverWait(driver, timeLimitInSeconds);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“desired xpath”)));

How can I achieve the similar functionality using android driver ?

Thanks in advance your time

Best Regards,
Rohit

Use the same :slight_smile: with proper xpath / id / name etc of the element to be visible. The element would be then mobileElement

Thank you so much for your reply.

I tried with the mobile Element option however, it it still giving me an exception after running it.
I’d be really grateful if you could help me correct my code.
Here is my code:

public static MobileElement mobileElement;

public static boolean waitForPresence(AndroidDriver driver, int timeLimitInSeconds, String targetResourceId){

	try{
		mobileElement =  (MobileElement) driver.findElementByAndroidUIAutomator("UiSelector().resourceId(\""+targetResourceId+"\")");
		WebDriverWait wait = new WebDriverWait(driver, timeLimitInSeconds);
		wait.until(ExpectedConditions.visibilityOf(mobileElement));
		isElementPresent = mobileElement.isDisplayed();
		return isElementPresent;	
	}catch(Exception e){
		isElementPresent = false;
		System.out.println(e.getMessage());
		return isElementPresent;
	}
	
}

Thank you in advance for your time!

Best regards,
Rohit

1 Like

Try this code

Thank you so much for your help.
The issue is resolved as I tried with your solution.

Best Regards,
Rohit