Using isDisplayed is throwing error like object not found

Hi,

I am trying to check whether the element is displayed in the page or not but instead of returning false when the element is not present in the page ,it is returning as element couldn’t be located and throwing error…

Code snippet:
try {
for (int counter = 1; counter < 4; counter++) {
System.out.println(“Searching for subsection…”);
horizontalSections.get(counter).click();

            if((subsectionHeader.isDisplayed()!=false)){
               dropDown.click();
                Assert.assertEquals(subsectionHeader.getText(),subsectionsList.get(0).getText(),"Subsection listed in header is selected in the subsection list as well");
            }
            else
            {
                System.out.println("Subsection name is not found");
            }
        }
    } catch (Throwable T) {
        System.out.println(T.getMessage());
        Assert.fail("Section list is not present");
    }

Error:
Can’t locate an element by this strategy: Locator map:

  • native content: “By.xpath: //XCUIElementTypeButton[@name=‘headerButton’]”
  • html content: “by id or name “subsectionHeader””
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: ‘2.53.0’, revision: ‘35ae25b1534ae328c771e0856c93e187490ca824’, time: ‘2016-03-15 10:43:46’
    System info: host: ‘Macbook-Pro.local’, ip: ‘169.254.176.123’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.12.4’, java.version: ‘1.8.0_91’
    Driver info: driver.version: unknown

isDisplayed <> isPresent :slight_smile: . when element NOT present it cannot be checked if it visible or not.
so first you need to check that element exists -> then to check if it visible on screen.

Thank you but to check the existence what will we be the method used?other than isdisplayed(),isenabled(),isselected()…?

if you did not found element any “.isXXX” are useless cause object NOT exist.

if i need to check same i do it like:

    @iOSXCUITFindBy(id = "someID")
    private List<IOSElement> mainContainer;

    public boolean isRequestLoaded() {
        System.out.println("  check 'Request' Screen loaded");
        boolean bool;
        setFastLookTiming();
        bool = !mainContainer.isEmpty();
        setDefaultTiming();
        return bool;
    }
    
    // where e.g. setFastLookTiming is:
   private int fastLook = 7; // wait for 7 sec MAX
   PageFactory.initElements(new AppiumFieldDecorator(driver, fastLook, TimeUnit.SECONDS), this);

okay thank you let me try it…:relaxed:

In this scenario, instead of using findElementById use findElementsById.
findElementsById returns an array of elements.
Sudo code will be something like:

el = driver.findElementsById(elementId)
if(el.length>0){
//It means element is found and you can perform operations on the element using
el[0].click()
}else{
//element not found
}

This way it wont throw error.

remember when this element is not found it will stack for your default timeout! which can be long :-). so just “driver.findElementsById(elementId)” is long operation without limiting it by time.

@Aleksei can you pls explain in a bit detail? I am confused.:relaxed:

@shashikumarraja
when you start driver you provide some default timeout to find elements e.g. 20sec OR if not then some default is using. or if you use page object pattern you also provide some default time which in normal case is up to 20-30sec.

now imaging you need quickly check that element NOT exist faster then this default waiting time. in mine code above i am limiting maximum time of search operation to 7sec (sometimes when i need i am limiting it up to 1 or 2sec). this helps to avoid useless time lost in test.

@Aleksei got it.
Then in that scenario
waitForElementsById(id, asserters.isDisplayed, timeout, pollFreq) should also be a good solution

I tried to use this solution, but the error message suggests removing redundant code.

public class BaseVerifyMemberPage {


    AppiumDriver driver;

    public BaseVerifyMemberPage(AppiumDriver driver) {
        this.driver = driver;
        PageFactory.initElements(new AppiumFieldDecorator(driver,7,TimeUnit.SECONDS), this);
    }

I am using page object pattern with page factory.