APPIUM IOS - Check if element exist on screen

When i check if element exist on screen like this :

if(driver.findElement(By.name(“OK”)).isEnabled())

I get an error message :

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 37 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

I need to check is the element (popup message with “OK” button) exist

Thanks !

use isDisplayed() instead of isEnabled()

Same Error :frowning:

An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)

This is the line :slight_smile:

if(driver.findElement(By.name(“Allow”)).isDisplayed() )
telemessage.general.tapMessageButtonByName(“Allow”);

Thanks !!

  1. Check manually first in attributes if visible is shown as true
  2. if yes then use… element.getAttribute(“Visible”) which will return the boolean value assigned to the element present in the screen

By Name is depreciated in latest Appium.
If you want to verify the String try it this way:

if(driver.findElement(By.xpath ("//*[@text=‘Allow’]").isDisplayed() )

Hope it works for you.

Cheers!

isDisplayed() for me will throw org.openqa.selenium.NoSuchElementException: Can’t locate an element by this strategy:… if the element is not present in the page.
I can only use it in assertion to validate it.
If I want to check if the element is not present and then continue the test I’m using this work around found here

public void checkIfElementIsPresent(MobileElement element1) {
WebDriverWait wait = new WebDriverWait(driver, 1);
try {
if (wait.until(ExpectedConditions.visibilityOf(element1)) != null) {
Assert.assertTrue(element1.isDisplayed());
//do something if present

		}
	} catch (Exception e) {
		log.info("Element not present, we are good here!");
                    //do something if not present
		// Assert.assertTrue(element2.isDisplayed());
	}
}

Thanks, i will try it ! :slight_smile:

I will try it ! thanks ! :slight_smile:

Great, I’ll try it

A small questions… can you please write me an example here how do you create an MobileElements ?

if i use this to click on Element with xPath :

driver.findElement(By.xpath("//XCUIElementTypeButton[@name=“Next”]")).click();

What will be my MobileElement ?

thanks !!!1

Well, my framework is with POM(Page Object Model)
I create the element in other class called Screens like this:
@AndroidFindBy(id = “dialog_later_button”)
public MobileElement laterButtonOverlay;

In your case you can do like this:
WebDriverWait wait = new WebDriverWait(driver, 1);
try {
if (wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpathExpression) != null) {
//not mandatory this like
Assert.assertTrue(driver.findElement(By.xpath ("//*[@text=‘Allow’]").isDisplayed());
//do something if present

		}
	} catch (Exception e) {
		log.info("Element not present, we are good here!");
                    //do something if not present
		}
}

Thank !!! Its work ! :slight_smile:

Hi what if I would like to repeat my step in a scroller until the element finds?

I am using the below code but it is not working, it just scroll once when it does not find first time and then not doing as expected and the value which I have to find is after 3 to 4 scroll,

what I am doing Is if the element is not found, catching it and scrolling again, after scrolling I am calling same function again to check, and if the element is find then I click that radio button,

public void scrollAndClickOnString() throws InterruptedException
{

	System.out.println("inside scroll function");
	
		new TouchAction((PerformsTouchActions) driver)
		.press(PointOption.point(240, 1648))
		.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
		.moveTo(PointOption.point(240, 1076))
		.release().perform();
		
		
		
		scrollTillWebView();

}

public void scrollTillWebView() throws InterruptedException {

WebDriverWait wait = new WebDriverWait(driver, 1);
	try {
	if (wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//android.view.View[@text='Please select the environment'])[2]//following::android.widget.RadioButton[@text='sit02']"))) != null) {
		System.out.println("inside if loop @@@@@@@@@@@@@@@@@@@@@@@@@@");
		
	Assert.assertTrue(driver.findElement(By.xpath("(//android.view.View[@text='Please select the environment'])[2]//following::android.widget.RadioButton[@text='sit02']")).isDisplayed());
	
	driver.findElement(By.xpath("(//android.view.View[@text='Please select the environment'])[2]//following::android.widget.RadioButton[@text='sit02']")).click();
			
	Thread.sleep(3000);
	lp.get_ok_button().click();
			}
		} catch (Exception e) {
			System.out.println("Element not present, we are good here!");
			
			scrollAndClickOnString();
		}
	

	
}