Unable to locate specific element using xpath (unless I'm in debug mode)

I am using appium 1.6.5. Tests are written in Intellij for iOS mobile phones.

I have an element defined as follows:
@iOSFindBy(xpath = “//XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeNavigationBar/XCUIElementTypeStaticText”)
public MobileElement titleBar_Name;

When I run “titleBar_Name.getText()” an exception is thrown. Because the element cannot be found.
So I created some extra code to loop through all “XCUIElementTypeNavigationBar/XCUIElementTypeStaticText” items. This code also fails.

But if I debug the code, then looping through the elements works and returns the correct result.
Not only that, I can see that the element is being displayed.
It is a generic element which is reused when different views are displayed. And every time it is found ok (with different labels) - except for this one time.

Trying to wait for a few seconds does not work either. I have tried waiting for 20 seconds.
Is it somehow possible to refresh the appium driver? So that it refreshes whats on screen?
You may have guessed by now I’m new to this. Though the code is existing code which has not been maintained or run for about 6 months.

Appium displays the following in the log when the error occurs:

[HTTP] --> POST /wd/hub/session/34164209-1a18-41fc-8ee9-2492a8ade564/elements {“using”:“xpath”,“value”:"//XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeNavigationBar/XCUIElementTypeStaticText"}
[debug] [MJSONWP] Calling AppiumDriver.findElements() with args: [“xpath”,"//XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeNavigationBar/XCUIElementTypeStaticText",“34164209-1a18-41fc-8ee9-2492a8ade564”]
[debug] [XCUITest] Executing command ‘findElements’
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, name, class name, -ios predicate string, -ios class chain, accessibility id
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [JSONWP Proxy] Proxying [POST /elements] to [POST http://localhost:8100/session/AB867EE9-989F-4D79-B51F-EA9A37632691/elements] with body: {“using”:“xpath”,“value”:"//XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeNavigationBar/XCUIElementTypeStaticText"}
[debug] [JSONWP Proxy] Got response with status 200: {“value”:"*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[1]\n\n(\n\t0 CoreFoundation 0x000000010327bd4b __exceptionPreprocess + 171\n\t1 libobjc.A.dylib 0x0000000102cdd21e objc_exception_throw + 48\n\t2 CoreFoundation 0x000000010318b783 -[__NSPlaceholderArray initWithObjects:count:] + 275\n\t3 CoreFoundation 0x0000000103192994 +[NSArray arrayWithObjects:count:] + 52\n\t4 XCTest 0x00000001026d14f7 -[XCElementSnapshot(Hitpoint) _transformPoint:windowContextID:windowDisplayID:] + 133\n\t5 XCTest 0x00000001026d18e9 -[XCElementSnapshot(Hitpoint) hitPoint] + 386\n\t6 WebDriverAgentLib 0x0000000113e2d09a -[XCElementSnapshot(FBIsVisible) fb_isVisible] + 810\n\t7 WebDriverAgentLib 0x0000000113e3808b -[XCElementSnapshot(WebDriverAttributes) isWDVisible] + 43\n\t8 WebDriverAgentL…
[debug] [MJSONWP] Responding to client with driver.findElements() result: []
[HTTP] <-- POST /wd/hub/session/34164209-1a18-41fc-8ee9-2492a8ade564/elements 200 581 ms - 74
[HTTP] --> GET /wd/hub/session/34164209-1a18-41fc-8ee9-2492a8ade564/context {}
[debug] [MJSONWP] Calling AppiumDriver.getCurrentContext() with args: [“34164209-1a18-41fc-8ee9-2492a8ade564”]
[debug] [XCUITest] Executing command ‘getCurrentContext’
[debug] [MJSONWP] Responding to client with driver.getCurrentContext() result: “NATIVE_APP”
[HTTP] <-- GET /wd/hub/session/34164209-1a18-41fc-8ee9-2492a8ade564/context 200 2 ms - 84
[HTTP] --> POST /wd/hub/session/34164209-1a18-41fc-8ee9-2492a8ade564/elements {“using”:“xpath”,“value”:"//XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeNavigationBar/XCUIElementTypeStaticText"}
[debug] [MJSONWP] Calling AppiumDriver.findElements() with args: [“xpath”,"//XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeNavigationBar/XCUIElementTypeStaticText",“34164209-1a18-41fc-8ee9-2492a8ade564”]
[debug] [XCUITest] Executing command ‘findElements’
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, name, class name, -ios predicate string, -ios class chain, accessibility id
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [JSONWP Proxy] Proxying [POST /elements] to [POST http://localhost:8100/session/AB867EE9-989F-4D79-B51F-EA9A37632691/elements] with body: {“using”:“xpath”,“value”:"//XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeNavigationBar/XCUIElementTypeStaticText"}
[debug] [JSONWP Proxy] Got response with status 200: {“value”:"*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[1]\n\n(\n\t0 CoreFoundation 0x000000010327bd4b __exceptionPreprocess + 171\n\t1 libobjc.A.dylib 0x0000000102cdd21e objc_exception_throw + 48\n\t2 CoreFoundation 0x000000010318b783 -[__NSPlaceholderArray initWithObjects:count:] + 275\n\t3 CoreFoundation 0x0000000103192994 +[NSArray arrayWithObjects:count:] + 52\n\t4 XCTest 0x00000001026d14f7 -[XCElementSnapshot(Hitpoint) _transformPoint:windowContextID:windowDisplayID:] + 133\n\t5 XCTest 0x00000001026d18e9 -[XCElementSnapshot(Hitpoint) hitPoint] + 386\n\t6 WebDriverAgentLib 0x0000000113e2d09a -[XCElementSnapshot(FBIsVisible) fb_isVisible] + 810\n\t7 WebDriverAgentLib 0x0000000113e3808b -[XCElementSnapshot(WebDriverAttributes) isWDVisible] + 43\n\t8 WebDriverAgentL…
[debug] [MJSONWP] Responding to client with driver.findElements() result: []
[HTTP] <-- POST /wd/hub/session/34164209-1a18-41fc-8ee9-2492a8ade564/elements 200 389 ms - 74
[HTTP] --> POST /wd/hub/session/34164209-1a18-41fc-8ee9-2492a8ade564/timeouts/implicit_wait {“ms”:1000}
[debug] [MJSONWP] Calling AppiumDriver.implicitWait() with args: [1000,“34164209-1a18-41fc-8ee9-2492a8ade564”]
[debug] [XCUITest] Executing command ‘implicitWait’
[debug] [BaseDriver] Set implicit wait to 1000ms
[debug] [MJSONWP] Responding to client with driver.implicitWait() result: null
[HTTP] <-- POST /wd/hub/session/34164209-1a18-41fc-8ee9-2492a8ade564/timeouts/implicit_wait 200 1 ms - 76
[HTTP] --> POST /wd/hub/session/34164209-1a18-41fc-8ee9-2492a8ade564/elements {“using”:“xpath”,“value”:"//XCUIElementTypeNavigationBar"}
[debug] [MJSONWP] Calling AppiumDriver.findElements() with args: [“xpath”,"//XCUIElementTypeNavigationBar",“34164209-1a18-41fc-8ee9-2492a8ade564”]
[debug] [XCUITest] Executing command ‘findElements’
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, name, class name, -ios predicate string, -ios class chain, accessibility id
[debug] [BaseDriver] Waiting up to 1000 ms for condition
[debug] [JSONWP Proxy] Proxying [POST /elements] to [POST http://localhost:8100/session/AB867EE9-989F-4D79-B51F-EA9A37632691/elements] with body: {“using”:“xpath”,“value”:"//XCUIElementTypeNavigationBar"}

I hope I have provided enough information…
Thanks in advance

Hi @jpc,
Even I experiencing the same problem. Can you raise this under appium issues so it can be address immedietly.

Thanks.