Not Able to switch to webView context in android

Not Able to switch to webView for the below mentioned scenario -:

Set up:
There is one webview inflated below. First Button loads url in the webview and second button launches same activity again.
Perform 2 tests to understand the issue.
Test 1:
step 1: click on first button and let the webpage load
step 2: click on the second button and launch the new activity
step 3: on the this launched screen click on the first button to load the webpage.
step 4: use Appium to switch the context to this webview on second page.
Expected: webview should switch. Result : webview is switched
Test 2
steps: Follow steps 2 to 4 only
Expected: Appium should switch to webview. Result: No switch/timeout exception as webview never switches.
Possible reason for test 2 failure: previous activity has a webview in just inflated state, no url is loaded there
APPIUM LOGS -:

before invocation : TC_Teamviewer_VerifyScreen
[HTTP] --> POST /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/se/log
[HTTP] {"type":"logcat"}
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Calling AppiumDriver.getLog() with args: ["logcat","373d3be5-2d1f-4942-a595-42a8105c4cd8"]
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Retrieving 'logcat' logs
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Responding to client with driver.getLog() result: [{"timestamp":1715755546905,"level":"ALL","message":"05-15 12:15:46.750 15843 15843 D SFHandler: handleMessage called() messageKey = 1 || scrolling banner"},{"timestamp":1715755547133,"level":"ALL","message":"05-15 12:15:46.976  1617  3905 I ActivityManager: Force stopping net.one97.paytm appid=10278 user=0: from pid 16824"},{"timestamp":1715755547144,"level":"ALL","message":"05-15 12:15:46.984  1617  3905 I ActivityManager: Killing 15843:net.one97.paytm/u0a278 (adj 0): stop net.one97.paytm due to from pid 16824"},{"timestamp":1715755547144,"level":"ALL","message":"05-15 12:15:46.986  1617  1757 D DisplayManagerService: Drop pending events for gone uid 10278"},{"timestamp":1715755547144,"level":"ALL","message":"05-15 12:15:46.987   835   835 W powerhal-libperfmgr: sched_setattr failed for thread 15965, err=3"},{"timestamp":1715755547144,"level":"ALL","message":"05-15 12:15:46.987   835   835 W powerhal-libperfmgr: sched_setattr failed for thread 15966, err=3"},{"timestamp":1715755547144,"level":"ALL","mess...
[HTTP] <-- POST /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/se/log 200 24 ms - 703457
[HTTP] 
[HTTP] --> POST /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/appium/app/launch
[HTTP] {}
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Calling AppiumDriver.launchApp() with args: ["373d3be5-2d1f-4942-a595-42a8105c4cd8"]
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] No app capability. Assuming it is already on the device
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 shell am start -W -n net.one97.paytm/net.one97.paytm.landingpage.activity.AJRMainActivity -S'
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Responding to client with driver.launchApp() result: null
[HTTP] <-- POST /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/appium/app/launch 200 3004 ms - 14
[HTTP] 
[HTTP] --> GET /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/context
[HTTP] {}
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Calling AppiumDriver.getCurrentContext() with args: ["373d3be5-2d1f-4942-a595-42a8105c4cd8"]
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Responding to client with driver.getCurrentContext() result: "NATIVE_APP"
[HTTP] <-- GET /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/context 200 3 ms - 22
[HTTP] 
[HTTP] --> GET /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/contexts
[HTTP] {}
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Calling AppiumDriver.getContexts() with args: ["373d3be5-2d1f-4942-a595-42a8105c4cd8"]
[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 shell cat /proc/net/unix'
[debug] [AndroidDriver] Parsed 1 active devtools socket: ["@webview_devtools_remote_17047"]
[debug] [AndroidDriver] Collecting CDP data of 1 webview
[debug] [AndroidDriver] Forwarding remote port webview_devtools_remote_17047 to a local port in range 10900..11000
[debug] [AndroidDriver] You could use the 'webviewDevtoolsPort' capability to customize the starting port number
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 forward tcp:10900 localabstract:webview_devtools_remote_17047'
[debug] [ADB] Removing forwarded port socket connection: 10900 
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 forward --remove tcp:10900'
[debug] [AndroidDriver] CDP data collection completed
[debug] [AndroidDriver] WEBVIEW_17047 mapped to pid 17047
[debug] [AndroidDriver] Getting process name for webview 'WEBVIEW_17047'
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 shell ps --help'
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 shell ps -A'
[debug] [AndroidDriver] Got process name: 'net.one97.paytm'
[debug] [AndroidDriver] Found 1 webview: ["WEBVIEW_net.one97.paytm"]
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Available contexts: ["NATIVE_APP","WEBVIEW_net.one97.paytm"]
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Responding to client with driver.getContexts() result: ["NATIVE_APP","WEBVIEW_net.one97.paytm"]
[HTTP] <-- GET /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/contexts 200 466 ms - 50
[HTTP] 
[HTTP] --> GET /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/contexts
[HTTP] {}
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Calling AppiumDriver.getContexts() with args: ["373d3be5-2d1f-4942-a595-42a8105c4cd8"]
[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 shell cat /proc/net/unix'
[debug] [AndroidDriver] Parsed 1 active devtools socket: ["@webview_devtools_remote_17047"]
[debug] [AndroidDriver] Collecting CDP data of 1 webview
[debug] [AndroidDriver] Forwarding remote port webview_devtools_remote_17047 to a local port in range 10900..11000
[debug] [AndroidDriver] You could use the 'webviewDevtoolsPort' capability to customize the starting port number
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 forward tcp:10900 localabstract:webview_devtools_remote_17047'
[debug] [ADB] Removing forwarded port socket connection: 10900 
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 forward --remove tcp:10900'
[debug] [AndroidDriver] CDP data collection completed
[debug] [AndroidDriver] WEBVIEW_17047 mapped to pid 17047
[debug] [AndroidDriver] Getting process name for webview 'WEBVIEW_17047'
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 shell ps -A'
[debug] [AndroidDriver] Got process name: 'net.one97.paytm'
[debug] [AndroidDriver] Found 1 webview: ["WEBVIEW_net.one97.paytm"]
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Available contexts: ["NATIVE_APP","WEBVIEW_net.one97.paytm"]
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Responding to client with driver.getContexts() result: ["NATIVE_APP","WEBVIEW_net.one97.paytm"]
[HTTP] <-- GET /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/contexts 200 402 ms - 50
[HTTP] 
Webview set: [NATIVE_APP, WEBVIEW_net.one97.paytm]
[HTTP] --> POST /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/context
[HTTP] {"name":"WEBVIEW_net.one97.paytm"}
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Calling AppiumDriver.setContext() with args: ["WEBVIEW_net.one97.paytm","373d3be5-2d1f-4942-a595-42a8105c4cd8"]
[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 shell cat /proc/net/unix'
[debug] [AndroidDriver] Parsed 1 active devtools socket: ["@webview_devtools_remote_17047"]
[debug] [AndroidDriver] Collecting CDP data of 1 webview
[debug] [AndroidDriver] Forwarding remote port webview_devtools_remote_17047 to a local port in range 10900..11000
[debug] [AndroidDriver] You could use the 'webviewDevtoolsPort' capability to customize the starting port number
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 forward tcp:10900 localabstract:webview_devtools_remote_17047'
[debug] [ADB] Removing forwarded port socket connection: 10900 
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 forward --remove tcp:10900'
[debug] [AndroidDriver] CDP data collection completed
[debug] [AndroidDriver] WEBVIEW_17047 mapped to pid 17047
[debug] [AndroidDriver] Getting process name for webview 'WEBVIEW_17047'
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 shell ps -A'
[debug] [AndroidDriver] Got process name: 'net.one97.paytm'
[debug] [AndroidDriver] Found 1 webview: ["WEBVIEW_net.one97.paytm"]
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Available contexts: ["NATIVE_APP","WEBVIEW_net.one97.paytm"]
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Connecting to chrome-backed webview context 'WEBVIEW_net.one97.paytm'
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] A port was not given, using random free port: 8000
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Passing web view details to the Chromedriver constructor: {
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]   "info": {
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     "Android-Package": "net.one97.paytm",
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     "Browser": "Chrome/124.0.6367.54",
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     "Protocol-Version": "1.3",
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     "User-Agent": "Mozilla/5.0 (Linux; Android 14; Pixel 6a Build/AP1A.240505.004; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/124.0.6367.54 Mobile Safari/537.36",
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     "V8-Version": "12.4.254.12",
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     "WebKit-Version": "537.36 (@cd0e5b0bca78ffe85d566cf44bc06658cf8b42ac)",
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     "webSocketDebuggerUrl": "ws://127.0.0.1:10900/devtools/browser"
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]   },
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]   "process": {
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     "name": "net.one97.paytm",
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     "id": "17047"
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]   }
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] }
[debug] [AndroidDriver] Precalculated Chromedriver capabilities: {
[debug] [AndroidDriver]   "androidPackage": "net.one97.paytm",
[debug] [AndroidDriver]   "androidUseRunningApp": true,
[debug] [AndroidDriver]   "androidProcess": "net.one97.paytm",
[debug] [AndroidDriver]   "androidDeviceSerial": "24071JEGR08810"
[debug] [AndroidDriver] }
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Before starting chromedriver, androidPackage is 'net.one97.paytm'
[debug] [Chromedriver@9cce] Changed state to 'starting'
[debug] [Chromedriver@9cce] Using the static Chromedriver->Chrome mapping
[debug] [Chromedriver@9cce] The most recent known Chrome version: 111.0.5563
[debug] [Chromedriver@9cce] Found 10 executables in '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac'
[debug] [Chromedriver@9cce] The following Chromedriver executables were found:
[debug] [Chromedriver@9cce]     '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac64_v124.0.6367' (version '124.0.6367.91', minimum Chrome version '124')
[debug] [Chromedriver@9cce]     '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac64_v123.0.6312' (version '123.0.6312.122', minimum Chrome version '123')
[debug] [Chromedriver@9cce]     '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac_arm64_v114.0.5735.90' (version '114.0.5735.90', minimum Chrome version '114')
[debug] [Chromedriver@9cce]     '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac_arm64_v114.0.5735.16' (version '114.0.5735.16', minimum Chrome version '114')
[debug] [Chromedriver@9cce]     '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac_arm64_v111.0.5563.64' (version '111.0.5563.64', minimum Chrome version '111.0.5563')
[debug] [Chromedriver@9cce]     '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac64_m1_v103.0.5060.134' (version '103.0.5060.134', minimum Chrome version '103.0.5060')
[debug] [Chromedriver@9cce]     '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac64_m1_v103.0.5060.53' (version '103.0.5060.53', minimum Chrome version '103.0.5060')
[debug] [Chromedriver@9cce]     '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac64_m1_v103.0.5060.24' (version '103.0.5060.24', minimum Chrome version '103')
[debug] [Chromedriver@9cce]     '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac64_m1_v91.0.4472.101' (version '91.0.4472.101', minimum Chrome version '91.0.4472')
[debug] [Chromedriver@9cce]     '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac64_m1_v91.0.4472.19' (version '91.0.4472.19', minimum Chrome version '91.0.4472')
[Chromedriver@9cce] Found 5 Chromedrivers, which are missing in the list of known versions: {"124.0.6367.91":"124","123.0.6312.122":"123","114.0.5735.90":"114","114.0.5735.16":"114","103.0.5060.24":"103"}
[Chromedriver@9cce] No mapping path provided
[debug] [Chromedriver@9cce] Browser version in the supplied details: Chrome/124.0.6367.54
[debug] [Chromedriver@9cce] Found Chrome bundle 'undefined' version '124.0.6367'
[debug] [Chromedriver@9cce] Found 1 executable capable of automating Chrome '124.0.6367'.
[debug] [Chromedriver@9cce] Choosing the most recent, '/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac64_v124.0.6367'.
[debug] [Chromedriver@9cce] If a specific version is required, specify it with the `chromedriverExecutable`desired capability.
[Chromedriver@9cce] Set chromedriver binary as: /Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac64_v124.0.6367
[debug] [Chromedriver@9cce] Killing any old chromedrivers, running: pkill -15 -f "/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac64_v124.0.6367.*--port=8000"
[Chromedriver@9cce] No old chromedrivers seem to exist
[debug] [Chromedriver@9cce] Cleaning this device's adb forwarded port socket connections: 24071JEGR08810
[debug] [ADB] List forwarding ports
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 forward --list'
[debug] [ADB] Removing forwarded port socket connection: 64433 
[debug] [ADB] Running '/Users/rajatgupta/Library/Android/sdk/platform-tools/adb -P 5037 -s 24071JEGR08810 forward --remove tcp:64433'
[Chromedriver@9cce] Spawning chromedriver with: /Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver_mac64_v124.0.6367 --port=8000 --adb-port=5037 --verbose
[debug] [Chromedriver@9cce] Chromedriver version: '124.0.6367.91'
[debug] [Chromedriver@9cce] Matched '/status' to command name 'getStatus'
[debug] [Chromedriver@9cce] Proxying [GET /status] to [GET http://127.0.0.1:8000/status] with no body
[Chromedriver@9cce] connect ECONNREFUSED 127.0.0.1:8000
[debug] [Chromedriver@9cce] Matched '/status' to command name 'getStatus'
[debug] [Chromedriver@9cce] Proxying [GET /status] to [GET http://127.0.0.1:8000/status] with no body
[debug] [Chromedriver@9cce] Got response with status 200: {"value":{"build":{"version":"124.0.6367.91 (51df0e5e17a8b0a4f281c1665dbd1b8a0c6b46af-refs/branch-heads/6367@{#984})"},"message":"ChromeDriver ready for new sessions.","os":{"arch":"arm64","name":"Mac OS X","version":"12.6.3"},"ready":true}}
[Chromedriver@9cce] Starting W3C Chromedriver session with capabilities: {
[Chromedriver@9cce]   "capabilities": {
[Chromedriver@9cce]     "alwaysMatch": {
[Chromedriver@9cce]       "goog:chromeOptions": {
[Chromedriver@9cce]         "androidPackage": "net.one97.paytm",
[Chromedriver@9cce]         "androidUseRunningApp": true,
[Chromedriver@9cce]         "androidProcess": "net.one97.paytm",
[Chromedriver@9cce]         "androidDeviceSerial": "24071JEGR08810"
[Chromedriver@9cce]       },
[Chromedriver@9cce]       "goog:loggingPrefs": {
[Chromedriver@9cce]         "browser": "ALL"
[Chromedriver@9cce]       }
[Chromedriver@9cce]     }
[Chromedriver@9cce]   }
[Chromedriver@9cce] }
[debug] [Chromedriver@9cce] Matched '/session' to command name 'createSession'
[debug] [Chromedriver@9cce] Proxying [POST /session] to [POST http://127.0.0.1:8000/session] with body: {"capabilities":{"alwaysMatch":{"goog:chromeOptions":{"androidPackage":"net.one97.paytm","androidUseRunningApp":true,"androidProcess":"net.one97.paytm","androidDeviceSerial":"24071JEGR08810"},"goog:loggingPrefs":{"browser":"ALL"}}}}
[debug] [Chromedriver@9cce] Webview version: 'Chrome/124.0.6367.54'
[Chromedriver@9cce] AxiosError: timeout of 240000ms exceeded
[Chromedriver@9cce]     at RedirectableRequest.handleRequestTimeout (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/axios/lib/adapters/http.js:614:16)
[Chromedriver@9cce]     at RedirectableRequest.emit (node:events:513:28)
[Chromedriver@9cce]     at Timeout.<anonymous> (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/follow-redirects/index.js:169:12)
[Chromedriver@9cce]     at listOnTimeout (node:internal/timers:559:17)
[Chromedriver@9cce]     at processTimers (node:internal/timers:502:7)
[debug] [Chromedriver@9cce] UnknownError: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: timeout of 240000ms exceeded
[debug] [Chromedriver@9cce]     at JWProxy.command (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/@appium/base-driver/lib/jsonwp-proxy/proxy.js:342:13)
[debug] [Chromedriver@9cce]     at runNextTicks (node:internal/process/task_queues:61:5)
[debug] [Chromedriver@9cce]     at listOnTimeout (node:internal/timers:528:9)
[debug] [Chromedriver@9cce]     at processTimers (node:internal/timers:502:7)
[debug] [Chromedriver@9cce]     at Chromedriver.startSession (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/lib/chromedriver.js:708:5)
[debug] [Chromedriver@9cce]     at Chromedriver.start (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/lib/chromedriver.js:631:7)
[debug] [Chromedriver@9cce]     at AndroidUiautomator2Driver.setupNewChromedriver (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/commands/context.js:471:3)
[debug] [Chromedriver@9cce]     at AndroidUiautomator2Driver.startChromedriverProxy (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/commands/context.js:217:10)
[debug] [Chromedriver@9cce]     at AndroidUiautomator2Driver.switchContext (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/commands/context.js:124:5)
[debug] [Chromedriver@9cce]     at AndroidUiautomator2Driver.setContext (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/commands/context.js:51:3)
[debug] [Chromedriver@9cce] Changed state to 'stopped'
[Chromedriver@9cce] Chromedriver exited unexpectedly with code null, signal SIGTERM
[Chromedriver@9cce] An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: timeout of 240000ms exceeded
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Encountered internal error running command: Error: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: timeout of 240000ms exceeded
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     at Object.wrappedLogger.errorAndThrow (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/@appium/support/lib/logging.js:115:34)
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     at Chromedriver.start (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-chromedriver/lib/chromedriver.js:659:16)
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     at AndroidUiautomator2Driver.setupNewChromedriver (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/commands/context.js:471:3)
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     at AndroidUiautomator2Driver.startChromedriverProxy (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/commands/context.js:217:10)
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     at AndroidUiautomator2Driver.switchContext (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/commands/context.js:124:5)
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)]     at AndroidUiautomator2Driver.setContext (/Users/rajatgupta/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/commands/context.js:51:3)
[HTTP] <-- POST /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/context 500 240936 ms - 854
[HTTP] 
[HTTP] --> POST /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/se/log
[HTTP] {"type":"logcat"}
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Calling AppiumDriver.getLog() with args: ["logcat","373d3be5-2d1f-4942-a595-42a8105c4cd8"]
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Retrieving 'logcat' logs
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Responding to client with driver.getLog() result: [{"timestamp":1715755572731,"level":"ALL","message":"05-15 12:16:12.576  4229  4229 E InputConnectionWrapper: \tat jml.a(PG:13)"},{"timestamp":1715755572731,"level":"ALL","message":"05-15 12:16:12.576  4229  4229 E InputConnectionWrapper: \tat jmn.af(PG:15)"},{"timestamp":1715755572731,"level":"ALL","message":"05-15 12:16:12.576  4229  4229 E InputConnectionWrapper: \tat jmh.w(PG:141)"},{"timestamp":1715755572731,"level":"ALL","message":"05-15 12:16:12.576  4229  4229 E InputConnectionWrapper: \tat jmh.y(PG:7)"},{"timestamp":1715755572731,"level":"ALL","message":"05-15 12:16:12.576  4229  4229 E InputConnectionWrapper: \tat jmh.c(PG:49)"},{"timestamp":1715755572731,"level":"ALL","message":"05-15 12:16:12.576  4229  4229 E InputConnectionWrapper: \tat jmh.m(PG:128)"},{"timestamp":1715755572731,"level":"ALL","message":"05-15 12:16:12.576  4229  4229 E InputConnectionWrapper: \tat dhi.l(PG:1249)"},{"timestamp":1715755572731,"level":"ALL","message":"05-15 12:16:12.576  4229  4229 E InputConnectionWrapper: \tat...
[HTTP] <-- POST /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/se/log 200 113 ms - 4552271
[HTTP] 
[HTTP] --> GET /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/screenshot
[HTTP] {}
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Calling AppiumDriver.getScreenshot() with args: ["373d3be5-2d1f-4942-a595-42a8105c4cd8"]
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Matched '/screenshot' to command name 'getScreenshot'
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Proxying [GET /screenshot] to [GET http://127.0.0.1:64662/session/0d1459cb-d2c3-4849-9a7f-c41f9810181f/screenshot] with no body
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Got response with status 200: {"sessionId":"0d1459cb-d2c3-4849-9a7f-c41f9810181f","value":"iVBORw0KGgoAAAANSUhEUgAABDgAAAlgCAYAAABtGofVAAABK2lDQ1BTa2lhAAAokX2QP0vDQBjGf5aCf6iD6OiQsYtSFbqoSxWLTlIjWJ3SNEmFtoYkpQhufgE/hODsKEJnBwVBcBQ/gji4xicJki71Pd673z33cHfvC4UZFMUK9PpR0KjXjJPmqTH9yZRGGpYd+kwOuX7eM+/byj++STHbdkJb65eyHehxXWmKl7yMOwm3Mr5KeBj5kfgm4cBs7IhvxWVvjFtjbPtB4n8Wb/W6Azv/NyWnf3ykdU+5zC7nhPh0sbjE4JANzXXtegyIxEM5IjqikIZOaiKTQI6+FBdHTNK/7InrD9gexXH8mGsHI7ivwtxDrpU3YaEETy+5lvfUtwIrlYrKguvC9x3MN2HxVfec/TVyQm1GWludCw1PtTlS9vVfm1XROhXWqP4CIJRN+z20bgYAAAAEc0JJVAgICAh8CGSIAAAgAElEQVR4nOzdeXxU5b0/8M85Z5bMZN8XEnaCIIQgChIqCFQpFmqtFf1pW+heem1fV7m1r3vbe9vealtrsdbba0utC2BduLVV0FpQFEUDKYatxGCAJGTft5nMfs7z+2MyQyaZ7DNJBj7v1wslJ+ec5zknbM8n3+d5JCGEABERERERERFRBJMnugNERERERERERGPFgIOIiIiIiIiIIh4DDiIiIiIiIiKKeAw4iIiIiIiIiCjiMeAgIiIiIiIioojHgIOIiIiIiIiIIh4DDiIiIiIiIiKKeAw4iIiIiIiIiCjiMeAgIiIiIiIioojHgIOIiIiIiIiIIh4DDiIiIiIiIiKKeAw4iIiIiIiIiCjiMeAgIiIiIiIioojHgIOIiIiIiIiIIh4DDiIiIiIiIiKKeAw4iIiIiIiIiCjiMeAgIiIiIiIioojHgIOIiIiIiIiIIh4DDiIiIiIiIiKKeAw4iIiIiIiI...
[debug] [AndroidUiautomator2Driver@9bdd (373d3be5)] Responding to client with driver.getScreenshot() result: "iVBORw0KGgoAAAANSUhEUgAABDgAAAlgCAYAAABtGofVAAABK2lDQ1BTa2lhAAAokX2QP0vDQBjGf5aCf6iD6OiQsYtSFbqoSxWLTlIjWJ3SNEmFtoYkpQhufgE/hODsKEJnBwVBcBQ/gji4xicJki71Pd673z33cHfvC4UZFMUK9PpR0KjXjJPmqTH9yZRGGpYd+kwOuX7eM+/byj++STHbdkJb65eyHehxXWmKl7yMOwm3Mr5KeBj5kfgm4cBs7IhvxWVvjFtjbPtB4n8Wb/W6Azv/NyWnf3ykdU+5zC7nhPh0sbjE4JANzXXtegyIxEM5IjqikIZOaiKTQI6+FBdHTNK/7InrD9gexXH8mGsHI7ivwtxDrpU3YaEETy+5lvfUtwIrlYrKguvC9x3MN2HxVfec/TVyQm1GWludCw1PtTlS9vVfm1XROhXWqP4CIJRN+z20bgYAAAAEc0JJVAgICAh8CGSIAAAgAElEQVR4nOzdeXxU5b0/8M85Z5bMZN8XEnaCIIQgChIqCFQpFmqtFf1pW+heem1fV7m1r3vbe9vealtrsdbba0utC2BduLVV0FpQFEUDKYatxGCAJGTft5nMfs7z+2MyQyaZ7DNJBj7v1wslJ+ec5zknbM8n3+d5JCGEABERERERERFRBJMnugNERERERERERGPFgIOIiIiIiIiIIh4DDiIiIiIiIiKKeAw4iIiIiIiIiCjiMeAgIiIiIiIioojHgIOIiIiIiIiIIh4DDiIiIiIiIiKKeAw4iIiIiIiIiCjiMeAgIiIiIiIioojHgIOIiIiIiIiIIh4DDiIiIiIiIiKKeAw4iIiIiIiIiCjiMeAgIiIiIiIioojHgIOIiIiIiIiIIh4DDiIiIiIiIiKKeAw4iIiIiIiIiCjiMeAgIiIiIiIioojHgIOIiIiIiIiIIh4DDiIiIiIiIiKKeAw4iIiIiIiIiCjiMeAgIiIiIiIioojHgIOIiIiIiIiIIh4DDiIiIiIiIiKKeAw4iIiIiIiI...
[HTTP] <-- GET /wd/hub/session/373d3be5-2d1f-4942-a595-42a8105c4cd8/screenshot 200 516 ms - 203364
[HTTP] 

io.appium.java_client.NoSuchContextException: An unknown server-side error occurred while processing the command. Original error: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: timeout of 240000ms exceeded
Build info: version: '4.7.0', revision: '0a5b49d16f'
System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '12.6.3', java.version: '17.0.9'
Driver info: io.appium.java_client.android.AndroidDriver$ByteBuddy$IoyW22lS
Command: [373d3be5-2d1f-4942-a595-42a8105c4cd8, switchToContext {name=WEBVIEW_net.one97.paytm}]
Capabilities {appium:--allow-insecure: chromedriver_autodownload, appium:adbExecTimeout: 150000, appium:appActivity: net.one97.paytm.landingpage..., appium:appPackage: net.one97.paytm, appium:autoLaunch: false, appium:automationName: UIAutomator2, appium:databaseEnabled: false, appium:desired: {--allow-insecure: chromedriver_autodownload, adbExecTimeout: 150000, appActivity: net.one97.paytm.landingpage..., appPackage: net.one97.paytm, autoLaunch: false, automationName: UIAutomator2, newCommandTimeout: 5000, noReset: true, platformName: android, platformVersion: 14, systemPort: 64662, udid: 24071JEGR08810}, appium:deviceApiLevel: 34, appium:deviceManufacturer: Google, appium:deviceModel: Pixel 6a, appium:deviceName: 24071JEGR08810, appium:deviceScreenDensity: 420, appium:deviceScreenSize: 1080x2400, appium:deviceUDID: 24071JEGR08810, appium:javascriptEnabled: true, appium:locationContextEnabled: false, appium:networkConnectionEnabled: true, appium:newCommandTimeout: 5000, appium:noReset: true, appium:pixelRatio: 2.625, appium:platformVersion: 14, appium:statBarHeight: 132, appium:systemPort: 64662, appium:takesScreenshot: true, appium:udid: 24071JEGR08810, appium:viewportRect: {height: 2073, left: 0, top: 132, width: 1080}, appium:warnings: {}, appium:webStorageEnabled: false, platformName: ANDROID}
Session ID: 373d3be5-2d1f-4942-a595-42a8105c4cd8

	at io.appium.java_client.remote.SupportsContextSwitching.context(SupportsContextSwitching.java:48)
	at io.appium.java_client.android.AndroidDriver$ByteBuddy$IoyW22lS.context$accessor$YFmfQwzc(Unknown Source)
	at io.appium.java_client.android.AndroidDriver$ByteBuddy$IoyW22lS$auxiliary$4NJwvVzG.call(Unknown Source)
	at io.appium.java_client.proxy.Interceptor.intercept(Interceptor.java:101)
	at io.appium.java_client.android.AndroidDriver$ByteBuddy$IoyW22lS.context(Unknown Source)
	at com.one97.paytm.appautomation.utils.ActionHelper.switchToContext(ActionHelper.java:3233)
	at com.one97.paytm.appautomation.utils.ActionHelper.switchToWebView(ActionHelper.java:3151)
	at testsuite.functional.anydesk.ExecutionSuite_Teamviewer.TC_Teamviewer_VerifyScreen(ExecutionSuite_Teamviewer.java:46)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
	at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:677)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:221)
	at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
	at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:969)
	at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:194)
	at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
	at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at org.testng.TestRunner.privateRun(TestRunner.java:829)
	at org.testng.TestRunner.run(TestRunner.java:602)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:437)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:431)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:391)
	at org.testng.SuiteRunner.run(SuiteRunner.java:330)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1256)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1176)
	at org.testng.TestNG.runSuites(TestNG.java:1099)
	at org.testng.TestNG.run(TestNG.java:1067)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Caused by: org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: timeout of 240000ms exceeded
Build info: version: '4.7.0', revision: '0a5b49d16f'
System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '12.6.3', java.version: '17.0.9'
Driver info: io.appium.java_client.android.AndroidDriver$ByteBuddy$IoyW22lS
Command: [373d3be5-2d1f-4942-a595-42a8105c4cd8, switchToContext {name=WEBVIEW_net.one97.paytm}]
Capabilities {appium:--allow-insecure: chromedriver_autodownload, appium:adbExecTimeout: 150000, appium:appActivity: net.one97.paytm.landingpage..., appium:appPackage: net.one97.paytm, appium:autoLaunch: false, appium:automationName: UIAutomator2, appium:databaseEnabled: false, appium:desired: {--allow-insecure: chromedriver_autodownload, adbExecTimeout: 150000, appActivity: net.one97.paytm.landingpage..., appPackage: net.one97.paytm, autoLaunch: false, automationName: UIAutomator2, newCommandTimeout: 5000, noReset: true, platformName: android, platformVersion: 14, systemPort: 64662, udid: 24071JEGR08810}, appium:deviceApiLevel: 34, appium:deviceManufacturer: Google, appium:deviceModel: Pixel 6a, appium:deviceName: 24071JEGR08810, appium:deviceScreenDensity: 420, appium:deviceScreenSize: 1080x2400, appium:deviceUDID: 24071JEGR08810, appium:javascriptEnabled: true, appium:locationContextEnabled: false, appium:networkConnectionEnabled: true, appium:newCommandTimeout: 5000, appium:noReset: true, appium:pixelRatio: 2.625, appium:platformVersion: 14, appium:statBarHeight: 132, appium:systemPort: 64662, appium:takesScreenshot: true, appium:udid: 24071JEGR08810, appium:viewportRect: {height: 2073, left: 0, top: 132, width: 1080}, appium:warnings: {}, appium:webStorageEnabled: false, platformName: ANDROID}
Session ID: 373d3be5-2d1f-4942-a595-42a8105c4cd8
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
	at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
	at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
	at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
	at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:262)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:541)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:596)
	at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:267)
	at io.appium.java_client.android.AndroidDriver$ByteBuddy$IoyW22lS.execute$accessor$YFmfQwzc(Unknown Source)
	at io.appium.java_client.android.AndroidDriver$ByteBuddy$IoyW22lS$auxiliary$jyiVQ5vT.call(Unknown Source)
	at io.appium.java_client.proxy.Interceptor.intercept(Interceptor.java:101)
	at io.appium.java_client.android.AndroidDriver$ByteBuddy$IoyW22lS.execute(Unknown Source)
	at io.appium.java_client.remote.SupportsContextSwitching.context(SupportsContextSwitching.java:45)
	... 34 more


Let me know will share the dummy apk for the same .

@mykola-mokhnach Can you please help.

make sure you can switch to web context using Chrome Remote Debugger. If that does not work then the above code won’t work as well.