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();
		}
	

	
}

I have used isDisplayed(),IsEnabled() method to identify Element in the page but its not identifying, Its giving error like this

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.

For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html

Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:17:03’

System info: host: ‘Mohammeds-Mini’, ip: ‘192.168.225.192’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘11.4’, java.version: ‘16.0.2’

Driver info: io.appium.java_client.ios.IOSDriver

used methods:
1)if ( driver .findElement(By. xpath ("//XCUIElementTypeImage[@name=“refresh”]")).isEnabled()) {

System. out .println(“Displayed”);

} else {

System. out .println(" Not Displayed");

}

2)if ( driver .findElement(By. xpath ("//XCUIElementTypeImage[@name=“refresh”]")).isDisplayed()) {

System. out .println(“Displayed”);

} else {

System. out .println(" Not Displayed");

}

  1. boolean displayed = element.isEnabled();

if (displayed= true ) {

System. out .println(“Displayed”);

} else {

System. out .println(" Not Displayed");

}

  1. boolean displayed = element.isDisplayed();

if (displayed= true ) {

System. out .println(“Displayed”);

} else {

System. out .println(" Not Displayed");

}

it is not working An element could not be located on the page using the given search parameters.

In 3rd point you need to compare
if(displayed == true){
System.out.println(“Displayed”);
}
see when you did boolean displayed=element.isEnabled(); then it assign the value true or false now you need to compare and for compare we use == operator.