Appium Mobile Safari Pop Up window - StaleElementReferenceError: Element does not exist in cache

I am running a Hybrid app.
Device : iPhone 14 with iOS 17.0
Appium: 2.4.1
Automation framework : XCUITest

Since its a hybrid app, the app is opened some operation is performed.
Then the mobile safari browser is launched and the flow is navigated to a portal which is running in Mobile Safari.
In the portal flow as a next step it opens up a pop up window. In the pop up window there is go button that i need to click.

However the its throwing a StaleElementReferenceError and no element in the popup window is getting idenitifed.

Here are some of the details

Total number of context: 3
Context-1 … NATIVE_APP - Mobile Application under test
Context-2… WEBVIEW_14710.24 - Main Window context
Context-3 … WEBVIEW_14710.26 — this is the current context for the pop up window

availableWindows … size() 2.
WindowHANDLES …[14710.24, 14710.26]

Element operated
driver.findElement(By.xpath("//*[@name=‘searchByIdButton’]")).click();

Error thrown

Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:17:03’
System info: host: ‘Mac-Q02H7RH7’, ip: ‘fda8:33c7:aae7:1:c7:1fe2:c27f:ef1b%en0’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.16’, java.version: ‘1.8.0_171’
Driver info: io.appium.java_client.ios.IOSDriver
Capabilities {app: /Users/x287202/Library/Deve…, autoAcceptAlerts: true, automationName: XCUITest, browserName: , clearSystemFiles: true, databaseEnabled: false, deviceName: iPhone 14, includeSafariInWebviews: true, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: false, newCommandTimeout: 180, platform: MAC, platformName: iOS, takesScreenshot: true, udid: 56870226-7243-4D5F-B7B3-32E…, webStorageEnabled: false}
Session ID: 04d6babb-3254-4f13-8b63-515466b57f8c
*** Element info: {Using=xpath, value=//*[@name=‘searchByIdButton’]}

Would like to highlight a difference here … this flow is working fine in Android/Mobile chrome browser.

The. noticable difference of the same code in Android side is
It has 2 context and 2 Window handles incompare to iOS app and mobile Safari browser flow were we had 3 context for 2 windows(separate context for main broswer and pop up window)

Android flow details
Context Length 2
Context-1 … NATIVE_APP
Context-2 … WEBVIEW_chrome (one context for both Main and Popup window)

Kindly help as i have spend enough time in debugging and troubleshooting this specific scenario

You did not mention: are you switching to Web Context? After switching you may work like in pure browser and use e.g. CSS selector instead of xPath.

Thanks for your quick response. No luck with CSS selector …
Change my locator from
driver.findElement(By.xpath("//*[@name=‘searchByIdButton’]")).click(); to the following

driver.findElement(By.cssSelector("#searchByIdButton"));

Yes i am changing the context from Native app to browser. Thats where i mentioned, for the same flow
In Android - i get 2 window and 2 context (one for native app and one for Web)
While
In iOS - i get 2 window and 3 context (one for native app and 2 for web - one for main browser and one for popup)

The error here is
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Remote debugger error with code ‘undefined’: StaleElementReferenceError: Element does not exist in cache
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:17:03’
System info: host: ‘Mac-Q02H7RH7’, ip: ‘fda8:33c7:aae7:1:c7:1fe2:c27f:ef1b%en0’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.16’, java.version: ‘1.8.0_171’
Driver info: io.appium.java_client.ios.IOSDriver
Capabilities {app: /Users/x287202/Library/Deve…, autoAcceptAlerts: true, autoWebview: true, autoWebviewTimeout: 20, automationName: XCUITest, browserName: , clearSystemFiles: true, databaseEnabled: false, deviceName: iPhone 14, includeSafariInWebviews: true, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: false, newCommandTimeout: 180, noReset: true, platform: MAC, platformName: iOS, safariAllowPopups: true, takesScreenshot: true, udid: 56870226-7243-4D5F-B7B3-32E…, waitForIdleTimeout: 20, webStorageEnabled: false}
Session ID: c407584e-c543-40bc-b15e-d8018ad8b645
*** Element info: {Using=css selector, value=#searchByIdButton}

Try first with browser if you can make same e.g. in pure safary on phone or mac

No Luck still the same … even if i dont load the aap… still getting context - 3 and windows-2
Here are the logs for you
availableWindows … size() 2
21 Feb 2024 11:42:59 INFO logger - availableWindows … [64717.1, 64717.4]
21 Feb 2024 11:42:59 INFO logger - Context Length 3 WEBVIEW_64717.4
21 Feb 2024 11:42:59 INFO logger - Context … NATIVE_APP
21 Feb 2024 11:42:59 INFO logger - Context … WEBVIEW_64717.1
21 Feb 2024 11:42:59 INFO logger - Context … WEBVIEW_64717.4
21 Feb 2024 11:42:59 INFO logger - Parent Window Context = WEBVIEW_64717.1
21 Feb 2024 11:43:06 INFO logger - >>>>>>>>>>>>>>>>>> WindowHANDLES …[64717.1, 64717.4]
21 Feb 2024 11:43:06 INFO logger - Title Window … CSS Crew Access for SWA PLT on February, 2024
21 Feb 2024 11:43:14 INFO logger - … Current Window title ----- CSS Crew Access for SWA PLT on February, 2024
21 Feb 2024 11:43:14 INFO logger - … Current context-child window ---- WEBVIEW_64717.4

Log
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Remote debugger error with code ‘undefined’: StaleElementReferenceError: Element does not exist in cache
Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:17:03’
System info: host: ‘Mac-Q02H7RH7’, ip: ‘fda8:33c7:aae7:1:c7:1fe2:c27f:ef1b%en0’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.16’, java.version: ‘1.8.0_171’
Driver info: io.appium.java_client.ios.IOSDriver
Capabilities {autoWebview: true, automationName: XCUITest, browserName: Safari, databaseEnabled: false, deviceName: iPhone 14, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: false, platform: MAC, platformName: iOS, takesScreenshot: true, udid: 56870226-7243-4D5F-B7B3-32E…, webStorageEnabled: false}
Session ID: 454290f1-5899-45d0-b2cb-8e2024c478e5
*** Element info: {Using=xpath, value=//*[@name=‘searchByIdButton’ and @value=‘Go’]}

Hi Aleksei,

Any update … has any one reported any such issue when it comes to a pop up with mobile safari browser with appium?

Kindly execute a simple scenario with mobile safari browser popup window to get a crux of the situation.

Try to replicate situation in pure browser. It will help you more understand problem

Hi Aleksei,

I have not seen any issue with desktop safari browser.
Its more specific to Mobile safari browser.