Problem with find Element

Hello everyone,

I am running appium 1.3.7 on an android application over a 4.3 device, i have encountred a problem when using findElement function. I want to test it on a List, the elements i want to find are visible. Appium doesn’t scroll to find these elements so the test fails, but when i scroll the list manually, appium detects them and the test passes.

Is that a normal behavior?

Thank you for you answers.

@sam_viz Yes its normal if you want to perform some action like click the element should be visible for capturing property or getting elements count its not a problem it can capture the properties for the elements which are not even in the visible region

@NishantSingh Thank you for your answer. I don’t think it captures the properties because Appium actually fails to detect the element, i have the following logs:
The weird symbols are normal because i am trying to find a name in french.

> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"name","selector":"Tél. fixe :","context":"","multiple":false}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"name","selector":"Tél. fixe :","context":"","multiple":false}}

info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding Tél. fixe : using NAME with the contextId: multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[DESCRIPTION=Tél. fixe :, INSTANCE=0]
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[TEXT=Tél. fixe :, INSTANCE=0]
info: [debug] [BOOTSTRAP] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
info: [debug] [BOOTSTRAP] [debug] Finding Tél. fixe : using NAME with the contextId: multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[DESCRIPTION=Tél. fixe :, INSTANCE=0]
info: [debug] Condition unmet after 10199ms. Timing out.
info: [debug] Responding to client with error: {“status”:7,“value”:{“message”:“An element could not be located on the page using the given search parameters.”,“origValue”:“No element found”},“sessionId”:“980cc042-9b31-40ef-a997-8a582615541b”}

I just need to check whether the element is there or not, not performing actions on it.

Thank you for your help

share example of your code where you try find element and tap on it

If the element have any unique “resourceId” property please use that as you are using “name” property to find it and name contains this weird symbol which can be a prob. and if still you want to use name property then check whether the copied name is same or not. Once I also worked for french site for desktop sites and sometime when we copy the symbol it gets changed So please check that

@amedvedev here is a sample from the code i am using


    public boolean checkElement(By element) {    
            try {
            driver.findElement(element);
            return true;
        } catch (NotFoundException e) {
            return false;
        }
    }

    @Test
    public void testUtilisateurAvecNumeroDeTelephonePortableInconnuAfficheTexteCorrespondant(){
     
        assertTrue(checkElement(By.name("Tél fixe: "));
        
    }

Thank you for your help

try:
public boolean checkElement(MobileBy element) {
try {
driver.findElement(element);
return true;
} catch (NotFoundException e) {
return false;
}
}

@Test
public void testUtilisateurAvecNumeroDeTelephonePortableInconnuAfficheTexteCorrespondant(){

assertTrue(checkElement(MobileBy.name("Tél fixe: "));

}

also your text has accents = you will have problem to find them with iOS :slight_smile:

we use follow to normalize text:
normalizedText = Normalizer.normalize(inputText, Normalizer.Form.NFD);

sometimes it is needed:
normalizedText = Normalizer.normalize(inputText, Normalizer.Form.NFD).replaceAll("\p{InCombiningDiacriticalMarks}+", “”);

more info: https://github.com/appium/appium/issues/4421

@NishantSingh I tought the problem would come from some encoding issue but it isn’t, i tried the same search (By.name) on another element which is in the visible area of the device and it worked. So i don’t think it’s related to that.
I only can work with name, because infortunately all the cells in the list have the same resourceId… So the only option left is for me to work with name search.

Do you have some Suggestions?
Thank you for your answer tough.

Problems on IOS? Thank you for giving me that hint, now i am prepared :smiley:
I am going to try your solution about MobileBy

no sure but maybe in Android also. anyway normalization greatly helps. we just created our compare strings function with normalization of text to avoid any such problems.

I am more than aware that normalization of text can avoid a lot of problems, but in my case, the problem is that appium don’t detect elements which are outside of the visible area of the device. I just tried on some normal text without any french accent, and the problem is still the same…

I just tried out using MobileBy, but it didn’t work …
I tought it would come frome using the By.name so i tried with By.xpath, and again it is working on all the elements which are in the visible area, but as soon as i try to fetch and element out of this area, i get the following error:

info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"Could not find an element using supplied strategy. "},"sessionId":"33ed715d-a54d-49d8-97b4-4b4abb8763b7"}

this time i just a line of code to test:

System.out.println(driver.findElement(MobileBy.xpath("//android.widget.ExpandableListView/android.widget.RelativeLayout[10]/android.widget.TextView[1]")).getText());

this code work for every i<10 as an index to the RelativeLayout…

Again, thank you for your answers.

with Android elements outside screen are not available to Appium.
with iOS elements outside can be available to Appium but you can’t tap on them.

So if you have some list of elements with Android and you are searching for some element you need to Scroll/Swipe list of elements to make not visible elements of list available.

1 Like

This is exactly the answer i wanted. Thank you !
Is there a way to scroll to a certain point of the screen? i just read the scrollTo function and it only accepts a string as the name of some element. And this is something i want to avoid…

with Android wrote own function scrollbyElementToSomeDirection.

with function we have Element (normally this is some element where list or elements are. in your case it is “//android.widget.ExpandableListView/android.widget.RelativeLayout[10]”) and direction as variables. inside function it is taking given element position and making swipe to needed direction.

e.g. resulting function:
((AppiumDriver) driver).swipe(startPoint.x , StartPoint.y, EndPoint.x, EndPoint.y, duration);

Thanks for the shared infos, appreciate it !
For the moment tough, i think i am going with the names, and externalise all the strings in an extern class.

Have a nice a day.
Thank you all for your answers.

Bro use this “driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);” it will sure work. You have to just make the script wait for few seconds to open the app on emulator or device and then run the script. just include that 1 line of code and you are good to go