How to check next condition if element not found

I am facing one issue that appium is not running program or not by passing element when element not present/not found.

My Scenario :

I am doing automation for one android app. In that I am working for profile picture. Now I have 2 options to get profile picture :

1 - Take picture from gallery
2 - Take profile using camera

So I am trying like :

1 - Get profile picture from gallery and do submit.

2 - Then I have put condition that if it do not get picture from gallery then take via CAMERA.

But here appium getting stuck and says element could not found when it do not get image from gallery.

My code :

 driver.findElement(By.id("com.test.free:id/iv_add_child")).click();

driver.findElement(By.id("com.test.free:id/tvSelectFromGallery")).click();

driver.findElement(By.xpath("//android.widget.TextView[@text='Recent']")).click();


//THIS IS GETTING PICTURE FROM GALLARY
WebElement gallaryphoto = driver.findElement(By.xpath("//android.widget.GridView//android.widget.ImageView[3]"));

gallaryphoto.click();

//HERE I AM CHECKING THAT IF PICTURE IS NOT IN GALLARY THEN CHECK HERE AND TAKE PICTURE USING CAMERA
if(gallaryphoto.isDisplayed())
	
{
	

	   driver.findElement(By.id("com.test.free:id/tvSubmit")).click();
	
}
else
{
			   driver.findElement(By.id("com.test.free:id/iv_add_child")).click();
	
	   driver.findElement(By.id("com.test.free:id/tvCapFromCam")).click();
	   
	   Thread.sleep(1000);
	   
	   driver.findElement(By.id("com.android.camera2:id/shutter_button")).click();
	   
	   driver.findElement(By.id("com.android.camera2:id/done_button")).click();
	   
	   
	   Thread.sleep(1000);
	   
	 driver.findElement(By.id("com.test.free:id/tvSubmit")).click();	
	
	
	
	
}

So finally as per above code it should take picture using CAMERA if not found any picture in gallary BUT appium stuck just before that condition because it is not getting picture from gallery.

Please help

iRang_QA.

//Here you are identifying the object
WebElement gallaryphoto = driver.findElement(By.xpath("//android.widget.GridView//android.widget.ImageView[3]"));

//In the next statement you are clicking in the above created object.
gallaryphoto.click();

//Because you are performing some operation, the screen may get refreshed and the identified object will become state and cannot be identified

gallaryphoto.isDisplayed() // here it cannot identify with the same object(become stale)

You need to reidentify again like:

gallaryphoto = driver.findElement(By.xpath("//android.widget.GridView//android.widget.ImageView[3]"));
Then use gallaryphoto.isDisplayed()

Like @UD saidā€¦

But one more thing about your logic, you are checking if you have any pic in the gallery? But with your code, in case you havenā€™t, it will throw an exception when it doesnā€™t find it, so you will never get to click on it.

It is still getting stuck and finding image infinite , I have changed code like :

 WebElement gallaryphoto = driver.findElement(By.xpath("//android.widget.GridView//android.widget.ImageView[3]"));



//HERE I AM CHECKING THAT IF PICTURE IS NOT IN GALLARY THEN CHECK HERE AND TAKE PICTURE USING CAMERA
if(gallaryphoto.isDisplayed())

{

           gallaryphoto.click();
	   driver.findElement(By.id("com.test.free:id/tvSubmit")).click();

} 
else

{

//CODE TO TAKE PICTURE VIA CAMERA
}

Still It is not going in else part and stuck in image finding.

What to do ??

1 Like

Yes Exactly , How can I pass through that exception and check condition further??

iRang,

better you have to use findElements method than findElement.

Syntax is like

 List<WebElement> gallaryphoto = driver.findElement(By.xpath("//android.widget.GridView//android.widget.ImageView[3]"));

if(gallaryphoto.size>0)
  //code belongs to true
else
  //code belongs to false
1 Like

UD , Thanks a lot. that is working now fine. But Now I am getting exception when condition become true :

"Exception in thread ā€œmainā€ java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.openqa.selenium.WebElement"

when it found image from gallry means when condition comes true. How can I click on single image from array. At this stage my code is :

 List<WebElement> gallaryphoto = driver.findElements(By.xpath(AppConstants.getgallerypic));




if(gallaryphoto.size()>0)
	
{
	
	   ((WebElement) gallaryphoto).click();  //HERE IT IS FIRING EXCEPTION
	   driver.findElement(By.id(AppConstants.submitbt)).click();
	
}

Which package you are referencing for ā€œListā€

It should be

"import java.util.List;"

UD - Its doneā€¦I have changed code as per belowā€¦

if(gallaryphoto.size()>0)

{

	   gallaryphoto.get(1).click();
          driver.findElement(By.id(AppConstants.submitbt)).click();

}

Now things working fineā€¦Thanks a lot for your support.

@UD - This time I have used ā€œListā€ technique , But if future if it comes to check for single element that how can we handle that?

Because I have just seen that when i use if condition and check for single element it there Or not , it is not coming till condition and going infinite to find that element which is not actually thereā€¦ So any standard solution for single element?

For appium or selenium, it doesnt know whether an element is present or not. So you need to include a proper looping with max wait time.

Pseudo code like:

time=0;
found=false
while(time<300) //300 seconds
{
 List<WE> ele=driver.findElements();
 if(ele.size()>0)
  found=true
  //exit loop
 else
  //say 2 sec.s or 5 secs. etcc...
  wait(5000);
  time=time+5;
}

if(found)
 //your true condition statements
else
 //your false condition statements
1 Like

P.S. - Using ruby, but easily adapted to any language

If you are sure the element should appear within the appium time out (30 seconds) then you can use

wait { find_element(...) }

It will look for element over and over again until the time out is reached, then it breaks (you can catch the exception if you want).

Following the while approach, then I normally do

count = 0
while find_element(...).size == 0
    sleep(5)
    count += 1
    if count > 10
        raise "Expecting to have found element XXX and didn't"
    end
end

okay , got your pointā€¦Thanks a lotā€¦

The technique Iā€™ve settled on is to handle the alternative in an exception:

try:
    element = driver.find_element_by_id("my first button")
    element.click()
except NoSuchElementException:
    element = driver.find_element_by_id("the other button")
    element.click()

The exception is thrown if the first button is not found, so the code to click or otherwise interact with it is skipped, and the code to handle the other button is run instead.

I really think find_element should return a status code rather than throw an exception. Maybe even control the behavior with a named parameter. There are just to many instances where you donā€™t necessarily know which element will appear, as that IS the test (does element x appear under y conditions).

Thank you. This technique really help me me in solving my problem

i got this error if multiple functionality testing in coding it some times works properly and some times showing such element not found errorā€¦ plz share how to resolve this error

Have tried it is stucked When element size is 0

Exception in thread ā€œmainā€ java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.openqa.selenium.WebElement
at NewQuote.main(NewQuote.java:87)

this is the exception i am getting can please anyone suggest i have added import java.util.List; still it is occurring.

I am trying to use the If statement for my code

Hi Rashmi,

Please post your code. From the error it looks like you are calling a method that returns an array, but you are trying to store the array in a WebElement.

try {
WebElement atherText = driver.findElementByXPath("//android.widget.TextView[@text=ā€˜Atherā€™]");
WebElement locationPopUp = driver.findElementByXPath("//*[@resource-id=ā€˜android:id/alertTitleā€™]");

	if(locationPopUp.getText().equals("Location access required") && atherText.getText().equals("Ather")){
		driver.findElementByXPath("//android.widget.Button[@text='OK']").click();
	}
	else if(atherText.getText().equals("Ather")){

// else {
driver.findElementByXPath("//android.widget.TextView[@text=ā€˜Atherā€™]").click();
driver.findElementByXPath("//android.widget.Button[@text=ā€˜ALLOWā€™]").click();
driver.navigate().back();

		// Accepting the permissions
		driver.findElementByXPath("//*[@text='ALLOW']").click();
		driver.findElementByXPath("//*[@text='ALLOW']").click();
		driver.findElementByXPath("//*[@text='ALLOW']").click();
		driver.findElementByXPath("//*[@text='ALLOW']").click();
		driver.findElementByXPath("//*[@text='ALLOW']").click();
		driver.findElementByXPath("//android.widget.Button[@text='OK']").click();
	}
	}
	catch(NoSuchElementException e) {
		logger.log().error("Element not located " + e);
	}

I got stuck in the condition check.
I need to jump between if and else condition based on the element presence.
I have tried many ways but unable to get the solution. Solution or suggestions will be appreciated