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.
//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()
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.
@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?
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).
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
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.
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.
// 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